0

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.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
laiboonh
  • 1,377
  • 1
  • 9
  • 19

1 Answers1

0

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!

E1000i
  • 1,409
  • 2
  • 11
  • 10
  • wow thanks man, this helps a lot. I did not know that subset function accepts size as criteria, this is not mentioned in the doco – laiboonh Sep 27 '12 at 01:59
  • If something ask for a bolean result you can put there whatever you want as long the result is a vector of bolean with the same size as the set you use against in any R function I thing. I just question of find the right condition ;) Great that helped you! – E1000i Sep 27 '12 at 10:39