I used randomForest( ) function in R. But R can't deal variables that more 32factor levels. But my factor variables are very important thing. So I want to use this variable. Then, how to deal this variable?

I know why randomForest( ) can't handle factor variable that have more 32 levels. But I'm wondering that how to use this categorical variable.

Binning the categorical variable??? or just delete?? ??


closed as off-topic by SmallChess, Peter Flom May 19 '17 at 11:26

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – SmallChess, Peter Flom
If this question can be reworded to fit the rules in the help center, please edit the question.


You can either try what Sainath said, or you can do one hot encoding of your variable, which will make a different column for each of your factor levels and the values in those columns would be 0 or 1 depending on whether that row contained that factor level. You can read how to do it here. I personally suggest doing one hot encoding of every factor variable regardless of whether it has more than 32 levels or not.

  • $\begingroup$ +1 This is what is called dummy variables in standard regression terminology. $\endgroup$ – MånsT May 19 '17 at 10:22

One way would be to combine the levels with low frequency into a single level. This will reduce the total number of levels, and may even bring it to <32 levels.

Another way would be to calculate the frequency of each level in the variable, and use this as a feature.

Yet another way is the Impact coding approach. For more details regarding the Impact coding technique, see this article http://www.win-vector.com/blog/2012/07/modeling-trick-impact-coding-of-categorical-variables-with-many-levels/.


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