문제

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!

도움이 되었습니까?

해결책

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]].

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top