我对R很新,所以如果这个问题比较新手,我道歉。我有一个大约33,000行的列表,显示订单ID和订单ID中订购的产品。结构如下:
Order.ID Product
193505 Pineapple Cheddar Burger
193505 Onion Rings
193564 Pineapple Cheddar Burger
193623 Hamburger
193623 French Fries
193623 Fountain Soda
193623 Hot Dog
193631 Hamburger
193631 French Fries
193631 Milkshake
193644 Daily Special
193726 Hamburger
193726 French Fries
193726 Fountain Soda
193728 Hamburger
193728 French Fries
193728 Fountain Soda
193738 Hamburger
193738 French Fries
193762 Hamburger
193762 French Fries
193762 Fountain Soda
193762 Hamburger
193762 French Fries
193762 Fountain Soda
我想生成一个相关矩阵,该矩阵反映了与每个其他项目在相同订单ID中订购的每个商品的相关性。我开始使用table(ID, Product)
,它给了我每个订单中订购的产品。现在我被困在如何计算每个其他产品(产品2-k)的订单数量,因为已经订购了产品1。理想情况下,我想对每个产品进行分析并将其转换为矩阵,但正确评估一个似乎是合乎逻辑的第一步。
我很感激我能解决这个问题的任何指导。提前谢谢。
答案 0 :(得分:5)
我建议您查看arules包(http://cran.r-project.org/web/packages/arules/index.html)。具体来说,您正在寻找频繁的项目集:
sets = apriori(asc, parameter=list(target="freq"))
它将能够找出所有组合。您将不得不更改数据格式,但table()应该是一个良好的开端。
它不仅可以返回2个产品的分布,还可以返回所有组合。
您正在寻找一组的支持,因此请使用:
inspect(sets)
答案 1 :(得分:1)
如果您使用reshape2包中的dcast,您将获得一个矩阵,然后您可以执行成对相关函数:
data <- read.table(h=T, text="Order.ID Product
+ 193505 'Pineapple Cheddar Burger'
+ 193505 'Onion Rings'
+ 193564 'Pineapple Cheddar Burger'
+ 193623 'Hamburger'
+ 193623 'French Fries'
+ 193623 'Fountain Soda'
+ 193623 'Hot Dog'
+ 193631 'Hamburger'
+ 193631 'French Fries'
+ 193631 'Milkshake'
+ 193644 'Daily Special'
+ 193726 'Hamburger'
+ 193726 'French Fries'
+ 193726 'Fountain Soda'
+ 193728 'Hamburger'
+ 193728 'French Fries'
+ 193728 'Fountain Soda'
+ 193738 'Hamburger'
+ 193738 'French Fries'
+ 193762 'Hamburger'
+ 193762 'French Fries'
+ 193762 'Fountain Soda'
+ 193762 'Hamburger'
+ 193762 'French Fries'
+ 193762 'Fountain Soda'")
library(reshape2)
df <- dcast(data, Order.ID ~ Product)
cor(df[,-1], use = "pairwise")
Daily Special Fountain Soda French Fries Hamburger Hot Dog
Daily Special 1.0000 -0.2868 -0.4375 -0.4375 -0.1250
Fountain Soda -0.2868 1.0000 0.8030 0.8030 0.2294
French Fries -0.4375 0.8030 1.0000 1.0000 0.1250
Hamburger -0.4375 0.8030 1.0000 1.0000 0.1250
Hot Dog -0.1250 0.2294 0.1250 0.1250 1.0000
Milkshake -0.1250 -0.2868 0.1250 0.1250 -0.1250
Onion Rings -0.1250 -0.2868 -0.4375 -0.4375 -0.1250
Pineapple Cheddar Burger -0.1890 -0.4336 -0.6614 -0.6614 -0.1890
Milkshake Onion Rings Pineapple Cheddar Burger
Daily Special -0.1250 -0.1250 -0.1890
Fountain Soda -0.2868 -0.2868 -0.4336
French Fries 0.1250 -0.4375 -0.6614
Hamburger 0.1250 -0.4375 -0.6614
Hot Dog -0.1250 -0.1250 -0.1890
Milkshake 1.0000 -0.1250 -0.1890
Onion Rings -0.1250 1.0000 0.6614
Pineapple Cheddar Burger -0.1890 0.6614 1.0000
答案 2 :(得分:0)
我认为这就是你想要的。首先,读入数据:
# Read in data
df<-read.table(
textConnection('
193505,Pineapple Cheddar Burger
193505,Onion Rings
193564,Pineapple Cheddar Burger
193623,Hamburger
193623,French Fries
193623,Fountain Soda
193623,Hot Dog
193631,Hamburger
193631,French Fries
193631,Milkshake
193644,Daily Special
193726,Hamburger
193726,French Fries
193726,Fountain Soda
193728,Hamburger
193728,French Fries
193728,Fountain Soda
193738,Hamburger
193738,French Fries
193762,Hamburger
193762,French Fries
193762,Fountain Soda
193762,Hamburger
193762,French Fries
193762,Fountain Soda
')
,sep=',')
names(df)<-c('id','food')
现在,构建矩阵。
# Create a matrix of 1's and 0's with id on left and food and column.
id.by.food<-as.matrix(table(df$id,df$food))
# If someone ordered an item twice, you'll get a '2'.
# Convert everything to 1's.
id.by.food<-pmin(id.by.food,1)
# Get a correlation matrix
round(cor(id.by.food),2)
Daily Special Fountain Soda French Fries Hamburger Hot Dog Milkshake Onion Rings
Daily Special 1.00 -0.32 -0.50 -0.50 -0.12 -0.12 -0.12
Fountain Soda -0.32 1.00 0.63 0.63 0.40 -0.32 -0.32
French Fries -0.50 0.63 1.00 1.00 0.25 0.25 -0.50
Hamburger -0.50 0.63 1.00 1.00 0.25 0.25 -0.50
Hot Dog -0.12 0.40 0.25 0.25 1.00 -0.12 -0.12
Milkshake -0.12 -0.32 0.25 0.25 -0.12 1.00 -0.12
Onion Rings -0.12 -0.32 -0.50 -0.50 -0.12 -0.12 1.00
Pineapple Cheddar Burger -0.19 -0.48 -0.76 -0.76 -0.19 -0.19 0.66
Pineapple Cheddar Burger
Daily Special -0.19
Fountain Soda -0.48
French Fries -0.76
Hamburger -0.76
Hot Dog -0.19
Milkshake -0.19
Onion Rings 0.66
Pineapple Cheddar Burger 1.00
答案 3 :(得分:0)