R项目组合

gre*_*099 3 combinations r count

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

有没有办法看到"苹果"+"香蕉"出现在三个名单(消费者1,2和3)上,"苹果"+"胡萝卜"出现在两个名单上(消费者1和4)?

Jot*_*ota 5

你可以在这里看到这些信息:

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
Run Code Online (Sandbox Code Playgroud)

要看到苹果与香蕉配对3次,胡萝卜配对2次,请查看第一行或第一列的下方.