user764186
user764186

Reputation: 961

Retrieving members from clusters (leafs)

Once you build up clusters using hclust, then cutree to specify the down-clusters; how can you retrieve the members forming the different clusters? Suppose you created a simple hierarchical cluster, what command can retrieve the elements "separately" figuring in leafs of the group of clusters?

I tried with table() but no way...

Upvotes: 1

Views: 1010

Answers (1)

flodel
flodel

Reputation: 89057

You can use the split function: for the second argument defining how data should be grouped, use your tree cut. It will return a list where each element is a different cluster.

hc <- hclust(dist(USArrests), "ave")
ct <- cutree(hc, k=3)

If you just want the members names:

split(names(ct), ct)
# $`1`
#  [1] "Alabama"        "Alaska"         "Arizona"        "California"   
#  [5] "Delaware"       "Florida"        "Illinois"       "Louisiana"     
#  [9] "Maryland"       "Michigan"       "Mississippi"    "Nevada"        
#  [13] "New Mexico"     "New York"       "North Carolina" "South Carolina"

# $`2`
#  [1] "Arkansas"      "Colorado"      "Georgia"       "Massachusetts"
#  [5] "Missouri"      "New Jersey"    "Oklahoma"      "Oregon"       
#  [9] "Rhode Island"  "Tennessee"     "Texas"         "Virginia"     
# [13] "Washington"    "Wyoming"      

# $`3`
#  [1] "Connecticut"   "Hawaii"        "Idaho"         "Indiana"      
#  [5] "Iowa"          "Kansas"        "Kentucky"      "Maine"        
#  [9] "Minnesota"     "Montana"       "Nebraska"      "New Hampshire"
# [13] "North Dakota"  "Ohio"          "Pennsylvania"  "South Dakota" 
# [17] "Utah"          "Vermont"       "West Virginia" "Wisconsin"    

or if you want the original data split by cluster:

split(USArrests, ct)
# $`1`
#                Murder Assault UrbanPop Rape
# Alabama          13.2     236       58 21.2
# Alaska           10.0     263       48 44.5
# Arizona           8.1     294       80 31.0
# [...]

# $`2`
#               Murder Assault UrbanPop Rape
# Arkansas         8.8     190       50 19.5
# Colorado         7.9     204       78 38.7
# Georgia         17.4     211       60 25.8
# [...]

# $`3`
#               Murder Assault UrbanPop Rape
# Connecticut      3.3     110       77 11.1
# Hawaii           5.3      46       83 20.2
# Idaho            2.6     120       54 14.2
# [...]

Upvotes: 2

Related Questions