Davis Davis - 1 year ago 177
R Question

Transform csv into transactions for arules

I have a subset from a database in csv which has several different columns and I would like to convert the data into transactions. I've already read this post


trans = read.transactions("data.csv", format = "single", sep = ",",
cols = c("EMAIL", "BRAND"))

However wasn't able to convert my data with the proposed solution:

shorts gap 1564 one@mail.x 1 1
tops gap 8974 one@mail.x 1 2
shoes nike 3245 two@mail.x 4 3
jeans levis 8956 two@mail.x 4 1

Now I want to use arules to understand what brands customers generally buy together. In order to use arules I need to convert my data so it looks as follows:

gap, gap
nike, levis

Can anybody help me figure out how to convert my data accordingly?

Answer Source

If we consider the column EMAIL as a sort of transaction ID, we can transform your data.frame to class transactions by:

trans <- as(split(df[,"BRAND"], df[,"EMAIL"]), "transactions")

# To explore the rules we could do
rules <- apriori(trans)
#  lhs        rhs     support confidence lift
#1 {levis} => {nike}  0.5     1          2   
#2 {nike}  => {levis} 0.5     1          2   
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download