Reputation: 23
Each person has multiple observations (rows) in my dataset--as many as 5 per person. I need to pull out a subset of people who have met my criteria during any single one of their observations, but without losing any of their observations. Essentially, it would be like this:
ID x
1 1
1 2
1 3
1 9
1 9
2 2
2 7
2 8
3 4
3 1
3 2
3 1
I would subset my data so that only people who had ever recorded a 1 for variable x would be kept. Person two would be excluded, but all observations for persons 1 and 3 would be kept in a new data frame.
ID x
1 1
1 2
1 3
1 9
1 9
3 4
3 1
3 2
3 1
Upvotes: 0
Views: 112
Reputation: 51592
Here is one way,
ind <- unique(df$ID[df$x==1])
df[df$ID %in% ind, ]
# ID x
#1 1 1
#2 1 2
#3 1 3
#4 1 9
#5 1 9
#9 3 4
#10 3 1
#11 3 2
#12 3 1
Upvotes: 1