rsortingarulesmarket-basket-analysis

Sort association rules by multiple columns in different order in r


I have transactional data and generated rules with arules package. I have difficulties sorting decreasing by confidence, increasing by chi and decreasing by support. How can I do this without converting to dataframe?

library(arules)
data(Groceries)
rules <- apriori(Groceries, parameter = list(support = 0.009, confidence = 0.25, minlen = 2))
quality(rules)$chi <- interestMeasure(rules, measure='chi', significance = TRUE, Groceries)

I've tried this but it didn't work:

ordered.rules <- sort(sort(sort(rules, by ="confidence", decreasing = TRUE), by ="chi", decreasing = FALSE), by = "support", decreasing = TRUE)
inspect(ordered.rules[1:5])

library(dplyr)
rules %>% arrange(desc(rules@quality$confidence),rules@quality$chi, 
desc(rules@quality$support))

Thanks in advance!


Solution

  • I think this should do the trick:

    ordered.rules2 <- sort(rules, 
      by = c(    "confidence", "chi",   "support"), 
      decreasing = c(TRUE,     FALSE,     TRUE))