Question

What's the easiest way to assign a cluster to a group in R? The functions utilized are:

hclust and cutree.

Basically, I want to assign one of the clusters created under cutree to an object.

Thanks!

Was it helpful?

Solution

cutree gives you a vector of cluster indices

hc <- hclust(dist(USArrests))
clusters.idx <- cutree(hc, k = 5) # create five clusters

head(clusters.idx)
#    Alabama     Alaska    Arizona   Arkansas California   Colorado 
#          1          1          1          2          1          2 

which you can use to split your original data:

clusters <- split(USArrests, clusters.idx)

Here, clusters is a list of data.frames. You can for example access the first cluster using clusters[[1]].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top