R item combinations

烂漫一生 提交于 2019-12-12 13:31:54

问题


I'm using R and want to find the most common pairs between consumers.

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)

Is there a way to see that "apple"+"banana" appears on three lists (consumers 1,2 and 3) and "apple"+"carrot" appears on two lists (consumers 1 and 4)?


回答1:


You can see that information here:

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

To see that apple pairs up with banana 3 times and carrot 2 times, look in the first row or the down the first column.



来源:https://stackoverflow.com/questions/35467871/r-item-combinations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!