Search code examples
rpart

Extracting Rpart rules to segment a dataset


Ive used Rpart to come up with decision tree, and using predict, i am able to apply the rules to see the predicted values. But I want to segment the dataset by the rules that have been generated. Essentially i want to label the rows in the dataset by the Rule/Rulenumber. how does one do this?


Solution

  • Almost a year since question was posted, but could be of help to others. The observations' node assignments in the rpart tree are saved in tree$where:

    library("rpart")    
    airq <- airquality[complete.cases(airquality),]
    tree <- rpart(Ozone ~ ., data = airq)
    tree$where