I've got the data with users and products they used over a certain time period:
dframe <- data.frame(id = c(1234,1234, rep(3456, 4)),
product = c("Apple", "Pear", "Apple", "Pear", "Grapes", "Kiwi"))
id product
1234 Apple
1234 Pear
3456 Apple
3456 Pear
3456 Grapes
3456 Kiwi
I'm looking for a way of creating unique combinations of product pairs, per user (where pair x-y would equal y-x pair). The solution would look like this:
solution
id product1 product2
1234 Apple Pear
3456 Apple Pear
3456 Apple Grapes
3456 Apple Kiwi
3456 Pear Grapes
3456 Pear Kiwi
3456 Grapes Kiwi
Essentially, I'd like to apply an equivalent of combn(product,2)
after dplyr's group_by(id)
, if that makes sense. Any ideas how to approach this?
Thanks a lot for your help!