match / find rows based on multiple required values in a single row in R

Viewed 1733

This must be a duplicate but I can't find it. So here goes.

I have a data.frame with two columns. One contains a group and the other contains a criterion. A group can contain many different criteria, but only one per row. I want to identify groups that contain three specific criteria (but that will appear on different rows. In my case, I want to identify all groups that contains the criteria "I","E","C". Groups may contain any number and combination of these and several other letters.

test <- data.frame(grp=c(1,1,2,2,2,3,3,3,4,4,4,4,4),val=c("C","I","E","I","C","E","I","A","C","I","E","E","A"))

> test
  grp val
1    1   C
2    1   I
3    2   E
4    2   I
5    2   C
6    3   E
7    3   I
8    3   A
9    4   C
10   4   I
11   4   E
12   4   E
13   4   A

In the above example, I want to identify grp 2, and 4 because each of these contains the letters E, I, and C.

Thanks!

2 Answers
Related