I have a dataset with the following variables (among ohter variables) that represents custome card transactions and I'm trying to cluster the clusters using k-means.

GENDER_M: can be 0, 1 or NA
GENDER_F: can be 0, 1 or NA

I'm trying K-means using R packages NbClust and cluster

Now, on this other question I wrote that hot encoding these variables didn't work out very well. I tried:

GENDER_M0: 1 for all the records that contain 0 in column GENDER_M - 0 otherwise
GENDER_M1: 1 for all the records that contain 1 in column GENDER_M - 0 otherwise
GENDER_F0: idem
GENDER_F1: idem

So, in total, I have 5 possible combinations:


1 means that there's a presence of the respective gender in the buying patters of the customer. For example, if a customer buys razors repeatedly, he will get a 1 in column GENDER_M.

Can anyone advise on a good method to deal with categorical variables in a clustering problem?


  • $\begingroup$ I think you will find the information you need in the linked thread. Please read it. If it isn't what you want / you still have a question afterwards, come back here & edit your question to state what you learned & what you still need to know. Then we can provide the information you need without just duplicating material elsewhere that already didn't help you. $\endgroup$ May 1, 2019 at 18:09
  • $\begingroup$ The generic K-Means is not directly applicable for categorical data because the distance metrics like Euclidean Distance does not make sense for categorical data. You could try looking into K-Modes Clustering which was introduced to deal with categorical variables while clustering. You might also want to look into the Gower Distance that can help deal with a mix of categorical and numerical variables. You will find implementation for both K-Modes and Gower Distance in R. $\endgroup$
    – frank
    May 1, 2019 at 18:15
  • $\begingroup$ Sure, I'll have a look at k-modes and figure out the best way to solve the problem. Thanks for your help $\endgroup$ May 1, 2019 at 18:17


Browse other questions tagged or ask your own question.