Suppose I have a data frame like this:
df <- data.frame(id = rep(c(1001:1004), each = 3), value = c(1,1,4,1,2,3,2,2,5,1,5,6))
df
id value
1 1001 1
2 1001 1
3 1001 4
4 1002 1
5 1002 2
6 1002 3
7 1003 2
8 1003 2
9 1003 5
10 1004 1
11 1004 5
12 1004 6
What is a good way to return me the IDs that have value equals to 1 but 3, i.e., any ID that has its corresponding value equals to 3 will be excluded but must have at least one value equals to 1? In this case, ID 1002 has 1 but also has 3 and shall be excluded. ID 1003 doesn’t have any value equals to 1 and shall be excluded too. So ID 1001 and 1004 will be returned. Thanks!
You can get the ID’s that contain 1 with
df$id[df$value == 1], and likewise for 3df$id[df$value == 3]. To exclude one set from the other, you can usesetdiff.In one command:
with(df,setdiff(id[value == 1],id[value == 3]))