Search code examples
rdecision-treerulesrpartrattle

asRules(tree) R save rules


I do have next trouble:

I created a decision tree with R based on rpart library, and since I have a broad list of variables, rules are and endeless list. By using asRules(tree) from rattle library, result is nicer than by just running tree once tree is computed.

The problem is the set of rules is longer than number of lines printeables from console, so I can't copy them by Control + C, and by saving this result into a variable, for instance:

t <- asRules(tree)

I would expect something like

 Rule number: 1 [target=0 cover=500 (4%) prob=0.8]
   var1 < 10
   var2 < 2
   var3 >=45
   var4 >=5

Eventhough result is

[1] 297 242 295 126 127 124

And obviously this isn't what I am looking for.

So I understand 3 ways of solving:

  1. Increasing limit of printable lines to access from console (I don't know how to do that).

  2. Print in console with a key press to continue, in order to first copy, then paste, and the pressing the button to get next results (I don't know how to do that either).

  3. Being able to save bunch of rules into a txt file or something similar instead of [1] 297 242 295 126 127 124.

Guys, any help is very much appreciated!

Thank you!


Solution

  • For #3 use

    sink(file='somefile.txt')
    asRules(tree)
    sink()