# Transform categorical variables for cluster analysis in R (mlr)?

Most clustering functions can't handle categorical variables. One way to deal with this is to transform them to binary integer variables (i.e. x in {0, 1}). This is straight forward for categorical variables with two categories (transformation to one binary integer variable with x in {0, 1}). Should I turn categorical variables with more than two categories into one binary integer variable per category, or should I also remove one category as reference category (which would mean that for some combinations of categories two binary variables have a different value and for others only one). Is there a sound theory behind that way of dealing with the issue of categorical variables when clustering (e.g. using kmeans) or is it only an emergency solution? Why does mlr not do this automatically (like for regression algorithms)?

• You can use the createDummyFeatures() function for this purpose. It doesn't happen automatically because you have a choice of method to use, or maybe you want to omit those features in your particular case. Not sure what you mean by "sound theory" -- cluster analysis is exploratory in nature, so there's no ground truth and no right or wrong answers. – Lars Kotthoff Sep 15 '17 at 16:04
• See stats.stackexchange.com/q/55798/3277. Usually there is no need to eliminate one dummy from the set in clustering task. – ttnphns Sep 17 '17 at 14:23