将列中的数据转换为R中的事务

时间:2016-04-06 10:55:34

标签: r

我有销售数据,即消费者购买的商品信息。我需要把它变成用于亲和力分析的交易数据。

数据是这样的:

ID=c("A123","A123","A123","A123","B456","B456","B456")
item=c("bread", "butter", "milk", "eggs", "meat","milk", "peas")

data.frame(cbind(ID, item))
    ID   item
1 A123  bread
2 A123 butter
3 A123   milk
4 A123   eggs
5 B456   meat
6 B456   milk
7 B456   peas

但我怎么能把它变成这种形式?:

  ID  basket
A123  bread,butter,milk,eggs
B456  meat,milk,peas

1 个答案:

答案 0 :(得分:5)

取决于你想做什么,也许

library(arules)
( trans <- as(split(df$item, df$ID), "transactions") )
# transactions in sparse format with
#  2 transactions (rows) and
#  6 items (columns)
inspect(trans)
#   items                    transactionID
# 1 {bread,butter,eggs,milk} A123         
# 2 {meat,milk,peas}         B456 

aggregate(item~ID, df, paste, collapse=",")
#     ID                   item
# 1 A123 bread,butter,milk,eggs
# 2 B456         meat,milk,peas