3

I have sales data ie info of what items consumers have bought. I would need to turn it into transactions data for affinity analysis.

Data is like this:

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

But how could I get it into this form?:

  ID  basket
A123  bread,butter,milk,eggs
B456  meat,milk,peas
ElinaJ
  • 791
  • 1
  • 6
  • 18
  • By the way, you'll want to avoid `data.frame(cbind(x))` since it will force all cols to be character. Let me know if there's a reason not to close your q as a duplicate of that one. – Frank Apr 06 '16 at 11:05
  • 2
    I think this is the dublicate: http://stackoverflow.com/questions/17313450/how-to-convert-data-frame-to-transactions-for-arules, but it was giving me an error: Error in as(split(df2[, 2], df2[, 1]), "transactions") : no method or default for coercing “list” to “transactions” However the error is not that it is a list, but the fact that I had not loaded arules. lukeA solved my problem – ElinaJ Apr 06 '16 at 12:07

1 Answers1

6

Depending on what you want to do, maybe

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 

or

aggregate(item~ID, df, paste, collapse=",")
#     ID                   item
# 1 A123 bread,butter,milk,eggs
# 2 B456         meat,milk,peas
lukeA
  • 53,097
  • 5
  • 97
  • 100