rcluster-analysiscategorical-datamixed-type

Clustering using gower distance in R


I have a dataframe which has categorical and numeric variables. I want to cluster this data using gower distance and get cluster values as a vector as in kmeans function. How can i achieve that?


Solution

  • You can use the vegan package to generate your gower matrix, and then create your clusters using the cluster package.

    gow.mat <- vegdist(dataframe, method="gower")
    

    Then you can feed that matrix into the PAM function. The example below will use the gower distance to generate 5 clusters

    clusters <- pam(x = gow.mat, k = 5, diss = TRUE)
    

    You can then get your cluster information from

    clusters$clustering