Search code examples
rrpartrattle

Save rpart decision tree rules to a dataframe


I am using rattle::asRules() to display rpart decision rules to the screen, my goal is to save the rules to a tidy data frame.

Here is an example:

library(rpart)
library(rattle)

fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
rattle::asRules(fit, TRUE)

Desired Output: enter image description here


Solution

  • In the end, I used the sink() function to diverts R output to a file.

    sink("rules.txt")
    rattle::asRules(fit, TRUE)
    sink()