The rpart function works even when a dataset has categorical predictor variables. You just have to ensure that the variable is tagged as a factor. See the following example:
> ed <- read.csv("education.csv") > ed$region <- factor(ed$region) > set.seed(1000) > t.idx <- createDataPartition(ed$expense, p = 0.7, list = FALSE) > fit <- rpart(expense ~ region+urban+income+under18, data = ed[t.idx,]) > prp(fit, type=2, nn=TRUE, fallen.leaves=TRUE, faclen=4, varlen=8, shadow.col="gray")
The following output is obtained:
Regression trees can also be built for categorical predictors as ...