Davis Davis - 3 months ago 33
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

library(arules)
library(arulesViz)

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


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

CATEGORY BRAND SKU EMAIL SEGMENT SALES
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

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

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

# To explore the rules we could do
rules <- apriori(trans)
inspect(rules)
#  lhs        rhs     support confidence lift
#1 {levis} => {nike}  0.5     1          2   
#2 {nike}  => {levis} 0.5     1          2   
Comments