Search code examples
rcluster-analysishierarchical

How do I know which observations belong to which cluster?


I ran hierarchical clustering in R, and how can I which observation belongs to which cluster? Thanks!

 ### Hierarchical Clustering
 d <- dist(EMEA_2, method = "euclidean") # distance matrix
 fit <- hclust(d, method="complete") 

 ### Decide bet number of clusters 
 library(knitr)
 library(NbClust)

 nc<-NbClust(data = EMEA_2, distance = "euclidean", min.nc=2, max.nc=15, method = "complete", index = "db", alphaBeale = 0.1)

 groups <- cutree(fit, k=2) # cut tree into 2 clusters

 ### Get group means and number of frequencies within each cluster
 a2<-aggregate(EMEA_2, list(groups),mean)
 a4<-data.frame(Cluster = a2[,1], Freq = as.vector(table(groups)), a2[,-1])

Solution

  • If you are interested in the optimisation result from NbClust, you will find it in nc$Best.partition where each number is the cluster number for respective row in the data matrix.

    for example

    > nc$Best.partition
     [1] 1 2 3 4 5 1 3 5 1 1 4 1 4 1 5 1 5 1 4 2
    

    for 20x10 data matrix.