Search code examples
rdataframedata-manipulationrpart

To get the tuples which follow a particular rule in a decision tree in R


I am creating a decision tree using rpart in R. I can also print out the rules generated by the decision tree using the path.rpart() function.

For the airquality data , i have the output of rules as

$`8`
[1] "root"          "Temp< 82.5"    "Wind>=7.15"    "Solar.R< 79.5"

$`18`
[1] "root"          "Temp< 82.5"    "Wind>=7.15"    "Solar.R>=79.5"
[5] "Temp< 77.5"   

$`19`
[1] "root"          "Temp< 82.5"    "Wind>=7.15"    "Solar.R>=79.5"
5] "Temp>=77.5"   

$`5`
[1] "root"       "Temp< 82.5" "Wind< 7.15"

and so on.

Is there a way i can write a code which puts these constraints on my initial table airquality to get the rows which follow these rules which would be equivalent to

airquality[which(airquality$Temp<82.5 & airquality$Wind>=7.15 & Solar.R<79.5)]

for the first rule.

Any help is greatly appreciated. Thanks in advance.


Solution

  • rules = rpart(airquality)    
    table(rules$where)
    airquality[rules$where==6,]
    

    will you give you the split data frame without coding the rules. I am not sure if that is what you are looking for.