dataframe - how to transform a data.frame into transactional data for arules in R? -
i have data.frame of following data:
rows: users
columns: different items coded binary
user item1 item2 itemm 1 0 1 1 2 0 1 1 3 1 1 1 4 1 0 1 n 1 1 1 how can transform data.frame transactional row-wise basket use "arules"-package in r?
thanks help!
hope helps!
library(arules) df <- data.frame( user = c(1,2,3,4), item1 = c(0,0,1,1), item2 = c(1,1,1,0), item3 = c(1,1,1,1)) l = lapply(apply(df[,-1], 1, function(x) names(df[,-1])[x==1]), function(x) paste(x,collapse = ",")) write(sapply(l, function(x) tostring(x)), file = "demo_basket") tr <- read.transactions("demo_basket", format = "basket", sep=",")
Comments
Post a Comment