将 csv 转换为 arules 的交易

Transform csv into transactions for arules

我有一个 csv 数据库的子集,它有几个不同的列,我想将数据转换为事务。我已经阅读 this post

library(arules)
library(arulesViz)

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

但是无法使用建议的解决方案转换我的数据:

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

现在我想用arules来了解客户通常会一起购买什么品牌。为了使用 arules,我需要转换我的数据,使其看起来如下所示:

gap, gap
nike, levis

谁能帮我弄清楚如何相应地转换我的数据?

如果我们将列 EMAIL 视为一种交易 ID,我们可以通过以下方式将您的 data.frame 转换为 class transactions

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