Quite a complicated task, to be honest. It's basically an extension of a question I asked earlier - Count unique values of a column by pairwise combinations of another column in R
Let's say this time around, I have the following data frame in R:
data.frame(Reg.ID = c(1,1,2,2,2,3,3), Location = c("X","X","Y","Y","Y","X","X"), Product = c("A","B","A","B","C","B","A"))
The data looks like this -
Reg.ID Location Product
1 1 X A
2 1 X B
3 2 Y A
4 2 Y B
5 2 Y C
6 3 X B
7 3 X A
I would like to count unique values of the column "Reg.ID" by pairwise combinations of the values in column "Product", grouped by the column "Location". The result should look like this -
Location Prod.Comb Count
1 X A,B 2
2 Y A,B 1
3 Y A,C 1
4 Y B,C 1
I tried getting the output using base R functions, but didn't get any success. I'm guessing there's a fairly simple solution using data.table
package in R?
Any help would be greatly appreciated. Thanks!