library(data.table) 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")) setDT(test)
Results in:
grp ACEI 1: 2 0 1 1 1 2: 4 1 1 2 1
Instead of returning group numbers, you can also βattachβ the received group numbers to the original data to show the raw data lines of each group that correspond to:
test[group.counts[I>0 & E>0 & C>0,], .SD, on="grp" ]
It shows:
grp val 1: 2 E 2: 2 I 3: 2 C 4: 4 C 5: 4 I 6: 4 E 7: 4 E 8: 4 A
PS: just to make the solution easier to understand: calculations for all groups:
> group.counts grp ACEI 1: 1 0 1 0 1 2: 2 0 1 1 1 3: 3 1 0 1 1 4: 4 1 1 2 1