在R中塑造apriori(市场篮子)分析的数据集

时间:2016-03-27 00:04:43

标签: apriori

我有一个如下所示的数据集:

Product  Customer  Visit_Time
1       140       Jan
2       140       Jan
1       184       Jan
4       140       Feb
8       192       Mar

如果我通过拆分(产品,客户)拆分数据,我最终将所有访问结合在一起(2月购买最终与1月购买一起)。

如何添加列以使其看起来像这样

OrderId  Product  Customer  Visit_Time
1          1       140       Jan
1          2       140       Jan
2          1       184       Jan
3          4       140       Feb
4          8       192       Mar

2 个答案:

答案 0 :(得分:0)

从数据创建交易的简便方法是:

> dat 
  Product Customer Visit_Time
1       1      140        Jan
2       2      140        Jan
3       1      184        Jan
4       4      140        Feb
5       8      192        Mar

> agg <- aggregate(Product ~ Customer + Visit_Time, data = dat, FUN = c)
> agg
  Customer Visit_Time Product
1      140        Feb       4
2      140        Jan    1, 2
3      184        Jan       1
4      192        Mar       8

> trans <- as(agg$Product, "transactions")
> transactionInfo(trans) <- agg[1:2]
> transactionInfo(trans)$transactionID <- 1:length(trans)

> inspect(trans)
  items Customer Visit_Time transactionID
1 {4}   140      Feb        1            
2 {1,2} 140      Jan        2            
3 {1}   184      Jan        3            
4 {8}   192      Mar        4       

答案 1 :(得分:0)

我意识到我可以通过粘贴感兴趣的变量来创建一个唯一的订单ID列,如下所示:

dat&lt; - transform(dat,order_id = paste0(Customer,Visit_Time,transactionID))