I have a table with two columns namely id
and item
:
df <- data.frame(id=c(1,1,2,2,2,2,3,3,3,4,4,4,4,4),item=c(1,2,3,1,2,3,4,1,2,3,1,2,1,2))
I want to find the most frequent combination (order doesn't matter) of 3 items per id
. So basically, n
choose r
where n = number of items within id
and r = 3
. The number of items per id
varies - some have more than 3, some have less.
I am new to R and read about combn
and expand.grid
, but I don't know how to use them in my case (to work within each id
).
"Find most frequent combination of values in a data.frame" is the closest question I found.
EDIT: The expected answer based on the example is the combination "1, 2, 3", which appears in id 2 and 4.