[R] converting a class dataframe (chars) to transaction class

Giorgio Garziano giorgio.garziano at ericsson.com
Thu Mar 31 22:09:31 CEST 2016


A more specific reproducible example.

set.seed(1023)
library(arules)

# starting from a dataframe whose fields are characters (see stringsAsFactors = FALSE), as asked
products <- c("P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10")
mydf <- data.frame(user = sample(LETTERS[1:20], 100, replace=T),
                   prod = sample(products, 100, replace=T),
                   stringsAsFactors=FALSE)
str(mydf)

# convert to factors
mydf$user <- factor(mydf$user)
mydf$prod <- factor(mydf$prod)

# splitting by user
prodlist <- split(x=mydf[,"prod"], f=mydf$user)
prodlist

# remove duplicates
prodlist <- lapply(prodlist, unique)
prod.trans <- as(prodlist, "transactions")
itemFrequency(prod.trans)

# generating rules
rules <- apriori(prod.trans,parameter=list(support=.1, confidence=.5))
inspect(rules)

--

Best,

GG





	[[alternative HTML version deleted]]



More information about the R-help mailing list