I have data in which few columns contain categorical data whereas remaining columns contain numerical data. I want to use random forest regressor from the randomForest library in r. So does this library work with such data out of the box (assuming I have preprocessed the data and is ready to directly feed in the regressor)?

DATA (something along this line) :

Target | product | status | revenue | MS
  0.8      abc       NC      1000    0.5
  0.5      abc       UR      200     0.2
  0.2      pqr       NC      800     0.04
  • 1
    $\begingroup$ You have to encode your categorical variables into numerical for most of trees including RandomForest, but I am not R user and cannot give you concrete guidelines. Just simply search how to encode categorical to numerical in R and you find tons of tutorials. $\endgroup$ Oct 31, 2018 at 12:41
  • $\begingroup$ I can always do that and I also know that if I use sklearn then I will have to convert categorical variable, but if randomForest library in R could do the job without converting then I would have less things to worry about. and regarding searching online I have already researched a ton but was not satisfied that is why Im here,Thanks for the comment! $\endgroup$ Oct 31, 2018 at 12:48
  • $\begingroup$ Ahun, in that case I am aware of a package that ONLY exists that can handle categorical encoding automatically. It called CatBoost, and I just checked it comes in R as well. I have experience using it, it is really great. It is a GBT (Gradient Boosting Tree). github.com/catboost/catboost $\endgroup$ Oct 31, 2018 at 12:54
  • $\begingroup$ Linear regression in R also handles categorical variables automatically $\endgroup$
    – keiv.fly
    Oct 31, 2018 at 14:03
  • $\begingroup$ have you read the help for the randomForest function in the randomForest package? It creates a forest for the iris data set, which has categorical and numerical variables. $\endgroup$
    – Spacedman
    Oct 31, 2018 at 14:10

1 Answer 1


In general you can always transform categorical data into numeric data. Many experienced STATA users need to do routinely. If you have gender variable coded as "male/female" you need one binary gendervvariable recorded as 0/1. If you have four levels variable such as main role in a football team "goalkeeper/defense/midfield/attack" you could take midfield as reference and generate the goalkeeper, midfield and attack variables all coded as 0/1. In caret, the dummyVars function allow to create easily dummy variables. Here an example with cylinders transformed as factors and then dummyfied:

 mtcars$cyl <- as.factor(mtcars$cyl)
 dmy<- dummyVars(" ~ .", data = mtcars, fullRank=T)
 dummy.mtcars <- data.frame(predict(dmy, newdata = mtcars))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.