R项目组合

R项目组合,r,count,combinations,R,Count,Combinations,我正在使用R,希望找到消费者之间最常见的配对 consumer=c(1,1,1,1,1,2,2,2,2,3,3,4,4,4,4,5) items=c("apple","banana","carrot","date","eggplant","apple","banana","fig","grape","apple","banana","apple","carrot","date","eggplant","apple") shoppinglists <- data.frame(consumer

我正在使用R,希望找到消费者之间最常见的配对

consumer=c(1,1,1,1,1,2,2,2,2,3,3,4,4,4,4,5)
items=c("apple","banana","carrot","date","eggplant","apple","banana","fig","grape","apple","banana","apple","carrot","date","eggplant","apple")
shoppinglists <- data.frame(consumer,items)
consumer=c(1,1,1,1,2,2,2,3,3,4,4,5)
项目=c(“苹果”、“香蕉”、“胡萝卜”、“枣”、“茄子”、“苹果”、“香蕉”、“无花果”、“葡萄”、“苹果”、“香蕉”、“苹果”、“胡萝卜”、“枣”、“茄子”、“苹果”)

ShoppingList您可以在这里看到这些信息:

tbl <- table(shoppinglists)
t(tbl) %*% tbl
#          items
#items      apple banana carrot date eggplant fig grape
#  apple        5      3      2    2        2   1     1
#  banana       3      3      1    1        1   1     1
#  carrot       2      1      2    2        2   0     0
#  date         2      1      2    2        2   0     0
#  eggplant     2      1      2    2        2   0     0
#  fig          1      1      0    0        0   1     1
#  grape        1      1      0    0        0   1     1

tbl这太好了,谢谢。不幸的是,我实际使用的数据有大约5000个不同的项目,因此无法检查该表。你对如何解决这个问题有什么想法吗?嗯,这取决于你想做什么,以及你想看到什么,但你可以输出一列:
tbl我想出来了:
tbl