Search code examples
rhierarchical-clustering

Cluster with `agnes`: how get Cluster Membership


I have a huge dataset and created the clusters with agnes as follows:

hc.res1 <- agnes(example, method = "complete")
plot(hc.res1, which.plots = 2, main = "Complete Linkage (agnes)")

enter image description here

I would like to separate the dataset into two clusters (one split). How can I do that with agnes? So I would like to get a list which looks like the following

| Datarow | Cluster |
| 1       | 1       |
| 2       | 2       |
| 3       | 1       |

Solution

  • You can use the cutree function to cut the tree into a number of k clusters. As no sample data were given, let's demonstrate this with a built-in data set iris that contains 3 groups:

    library(cluster)
    
    example <- iris # sample data set from R
    hc.res1 <- agnes(example, method = "complete")
    plot(hc.res1, which.plots = 2, main = "Complete Linkage (agnes)")
    
    # let's assume k=3 clusters, then cutree shows the groups
    cutree(k=3, hc.res1)
    

    The numbers returned by cutree correspond to the row numbers of the original data set.