Jonathan Rhein
Jonathan Rhein

Reputation: 1723

r: Obtaining final cluster results in a table/ dataframe when using hclust()

I am conducting a hierarchical cluster analysis by using hclust()

The code looks like this:

hc <- hclust(dist(USArrests), "ave")

Now, all I need is to get a table (or something equivalent) which contains all clusters and the observations (by their rowname, NOT number) which belong to them so that I can save it to some overall file/dataframe - e.g. Excel. (I want to run the hclust several times with different methods, variables and evaluate the results in the end.)

I now, its probably pretty easy, but I am stuck somehow... do you have any advice?

Ps.: I would also like to know how this works when using kmeans()

Upvotes: 1

Views: 4222

Answers (1)

Ananta
Ananta

Reputation: 3711

hclust does not give you the cluster groups. you can use cutree to make cluster them. So, if you want to cluster them in three groups;

cutree(hc,3) 

if you want dataframe,

data.frame(cutree(hc,3)) 

for kmeans,

km<-kmeans(USArrests,3)

km$centers

Upvotes: 5

Related Questions