Reputation:
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!
Upvotes: 2
Views: 880
Reputation: 89057
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]]
.
Upvotes: 3