【发布时间】:2017-05-23 13:32:36
【问题描述】:
我得到了他们在特定时间段内使用的用户和产品的数据:
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
我正在寻找一种方法来为每个用户创建独特的产品对组合(其中 x-y 对等于 y-x 对)。解决方案如下所示:
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
基本上,如果有意义的话,我想在 dplyr 的 group_by(id) 之后应用 combn(product,2) 的等效项。有什么想法可以解决这个问题吗?
非常感谢您的帮助!
【问题讨论】:
-
这是一个错字,现已更正,谢谢!
标签: r dataframe dplyr combinations