I have a dataframe:
df <- data.frame(
Group=c('A','A','A','B','B','B'),
Activity = c('Act1','Act4', 'Act3','Act1', 'Act2','Act3')
)
I want to filter for those groups only which contain both activities Act1
and Act2
. The following code returns zero values:
df %>% group_by(Group) %>% filter(Activity == 'Act1' & Activity == 'Act2')
If I use df %>% group_by(Group) %>% filter(Activity %in% c('Act1' , 'Act2') )
, it also returns group A, which I don't need.
How can I get only those groups that necessarily contain both the activities?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…