Two things come to mind:
Solution 1:
# Convert to a dendrogram object
hor.dendro <- as.dendrogram(hr)
# Get values for the first branch
m.1 <- m[unlist(hor.dendro[[1]]),]
Solution 2:
# Cut the tree in 2
tree.cut <- cutree(hr, 2)
# Get the ids for cluster #1
clust.1 <- which(tree.cut==1)
# Get the values from m
m.1 <- m[clust.1,]
In a more generalised manner, you may want to use one of the *apply
functions.
For instance:
clusters <- lapply(unique(tree.cut), function(grp)
{
m[which(tree.cut==grp),]
})
This returns (calling cutree
with 2 groups)
[[1]]
1 2 3 4 5
A 0 0 0 0 1
B 0 0 0 1 1
C 0 0 1 1 1
D 0 0 1 1 0
I 0 1 1 1 0
[[2]]
1 2 3 4 5
E 1 0 0 0 0
F 1 1 1 0 0
G 0 1 1 0 0
H 0 1 1 0 0
J 1 1 1 0 1
You can access the results with the [[ ]]
operator, such as: clusters[[2]]
to get the second cluster.