问题
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