Pre-Data processing categorical variables in decision tree in R (rpart) I am new to decision trees, there are several things confused me a lot.

*

*The first thing is that should we convert all categorical variables (Such as: gender, department, number of years) to a numeric variables using unclass method?


*At the first time, I run the decision tree model simple using rpart(Y~.) based on the raw data which contains lots of ord. Factors, Factors,int. I took a look at the cptable for this model.
After I convert all factor variables into numeric using unclass, and build a new model based on the dataframe. I don't know why it can produce a totally different cptable. What I have done is just convert all factors to numeric, that is. Will it influence the model result? Why ?


*The last question is that if I am making a predictive model using regression trees, should I scale the data?

 A: *

*You should convert any variables that should be numeric to numeric, e.g., number of years. It does not make sense to convert department to numeric. Any category that contains only two values (possibly gender, depending whether you work with two or more genders) can be left as is or changed to numeric, it does not make a difference to the tree, although it will be easier to interpret as categoric.

*Of course changing variables between categoric and numeric will change everything. If you leave number of years as categoric, then a tree will split it by assigning some numbers to the left child node and some to the right one - and probably using a wild mixture of assignments (0, 1, 2 to the left, 3 to the right, 4 and 5 to the left, 6 to the right...). If this variable is numeric, the tree will choose a threshold and assign every value below that threshold to the left and every number above the threshold to the right child.

*Scaling is possible, but not necessary. It will just change the threshold used in splitting. (If you have very large or small numbers, you may have numerical issues, but that is rare.)

