Search code examples
rarules

Subset arules transactions object according to transaction length


Anyone knows how to subset and arules transactions object according to transaction length?

For example:

library(arules)
data(Adult)
summary(Adult)

I want to subset Adult into different transactions objects based on the length of each transaction.


Solution

  • I recommend to take a look at package help as first stop. 'help.start()'. Navigate to packages and take a moment to see which methods are implemented for the package.

    It took a minute to discover that there is a size() and subset() implementation for transaction objects. So its very simple to do what you ask for.

    Here is how i will do it:

    #you can get a vector of the sizes of the transactions:
    sizes<-size(Adult)
    #If you want to automate the sunsetting with a for loop 
    #you can save which sizes are.
    size.labels<-as.numeric(levels(as.factor(sizes)))
    #Now you just need to use subset() function for 
    #arules using size as condition. 
    #Here few examples
    Adult.subset<-subset(Adult,sizes==size.labels[1]) #this for using in a loop maybe?
    Adult.subset.10<-subset(Adult,sizes==10)
    ...
    

    Hope it helps you!