我正在使用 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)
有没有办法看到“苹果”+“香蕉”出现在三个列表(消费者 1,2 和 3)上,“苹果”+“胡萝卜”出现在两个列表(消费者 1 和 4)上?
最佳答案
您可以在此处查看该信息:
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
要查看苹果与香蕉配对 3 次,胡萝卜与胡萝卜配对 2 次,请查看第一行或第一列的下方。
关于R 项目组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35467871/