Transform csv into transactions for arules [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-08 03:56:31

问题


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?


回答1:


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   


来源:https://stackoverflow.com/questions/39140760/transform-csv-into-transactions-for-arules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!