Search code examples
rarules

arules isolating lhs and rhs


So here is the example:

library(arules)
data(Adult)
rules <- apriori(Adult,parameter = list(maxlen = 2,minlen=2))
inspect(head(rules,3))


 lhs                         rhs                              support confidence     lift
1 {relationship=Unmarried} => {capital-loss=None}            0.1019819  0.9719024 1.019537
2 {occupation=Sales}       => {race=White}                   0.1005282  0.8920785 1.043314
3 {occupation=Sales}       => {native-country=United-States} 0.1039679  0.9226017 1.028055

I want to create a data.table with 2 columns, one called lhs and the other rhs where I have stored the lhs and rhs values of my rules like this:

lhs                        rhs
relationship=Unmarried    capital-loss=None
occupation=Sales          race=White
occupation=Sales          native-country=United-States

I would swear that I did it once starting with a<-as.data.table(inspect(rules)) on windows but on my mac that doesnt work... What do you suggest?


Solution

  • Given

    library(arules)
    library(data.table)
    data(Adult)
    rules <- apriori(Adult,parameter = list(maxlen = 2,minlen=2))
    r <- head(rules,3)
    

    If as.data.table(inspect(r)) does not work (it does on my machine, Win7 x64, R 3.2.2 & packageVersion("arules") beeing 1.3.0), then maybe try

    f <- function(x, fun) unlist(as(fun(x), "list"))
    ( dt <- data.table(lhs=f(r, lhs), rhs=f(r, rhs)) )
    #                       lhs                          rhs
    # 1: relationship=Unmarried            capital-loss=None
    # 2:       occupation=Sales                   race=White
    # 3:       occupation=Sales native-country=United-States