# Factors or dummy variables? Which approach is better for Machine Learning in R?

I am a beginner in Machine Learning. I have been inspecting some kernels at Kaggle. Some of these answers use factors in their predictive models while others split them into dummy variables (this happens especially when the code is written in Python). I am using R. Which approach is better?

• I think that when you use factors, then R automatically transforms these to dummies ? – user83346 May 3 '17 at 9:35
• It depends on the model. My guess is that internally does not matter because each model converts factors to dummy variables. For example, the rpart package (decision tree model) gives you dummy variables when looking at variables importance. But Random Forest gives you factors. So I think that as long as you classify the categorical variables correctly (i.e. as factors), it does not really matter. – Antonio Serrano May 3 '17 at 10:14

It does not really matter. Factors are internally coded as Dummy variables in R. If you are not familiar with statistics and econometrics I suggest you to use factor variables (be sure that they are correctly coded with

factor(x, levels)


I suggest you this because you may include all dummy variables in the model and cause multicollinearity. In addition to this, you do not have to bother about creating the dummy coding, you can save up some lines of code.

If you want more information on this you can look here, here or here. In case you decide to recode it as dummy variables you can have a look here or here.

Hope this helps! Un saludo! ;)

• Exactly. That's what I thought. But I wanted to confirm it. Many thx! Muchas gracias! – Antonio Serrano May 3 '17 at 10:15
• @AntonioSerrano If you liked the answer don't forget to upvote it! Muchas gracias, cualquier duda me comentas! :) – adrian1121 May 4 '17 at 8:37
• I tried that yesterday @adrian1121. But since I am new at Cross Validated, the system does not allow me to upvote yet. It says "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Can't I export my profile from Stack overflow to Cross Validated? – Antonio Serrano May 4 '17 at 9:02
• Sure don't worry! You can export your profile but not your points and badges. Every forum is independent from each other. – adrian1121 May 4 '17 at 9:09
• Got it. When I get the necessary points, I will upvote your answer. ¡Gracias de nuevo! – Antonio Serrano May 4 '17 at 9:11