Reputation: 11833
I have a dataframe like below. Some of the subject/variable group do not have D1
in visit
. What I need to do is filter those rows. In other word, I need to extract only those groups that contains D1
in visit
. Appreciate it if anyone can help with this.
dt = expand.grid(subject=1:3,
variable = c('A', 'B', 'C'),
visit = c('D1', 'D2', 'D3'))
dt <- dt %>% filter(!(variable=='C'&visit=='D1'))
dt %>%
group_by(subject, variable) %>%
arrange(subject, variable) %>%
?????
Upvotes: 1
Views: 128
Reputation: 7592
You can use any
:
dt %>%
group_by(subject, variable) %>%
filter(any(visit=="D1"))
Upvotes: 1
Reputation: 887891
We can use %in%
dt %>%
group_by(subject, variable) %>%
filter('D1' %in% visit)
Upvotes: 2