I have a dataset with an ID column and an item column. An ID is mapped to one or more items. The dataset has a row for each item mapped to an ID. I want to return IDs that contain my_items. The order of the items does not matter. I have a toy example below.
ID <- c(1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5)
item <- c("a", "b", "c", "a", "b", "c", "d", "a", "b", "d", "b", "a", "c")
df <- data.frame(cbind(ID, item))
df
my_items <- c("a", "b", "c")
My expected output would only include item ID 1 and 5.
>Solution :
df %>%
group_by(ID) %>%
filter(setequal(item,my_items))
Output
ID item
<chr> <chr>
1 1 a
2 1 b
3 1 c
4 5 b
5 5 a
6 5 c
