I am doing a cluster analysis and I want to count the number of occurences of a certain variable in a leaf of a pruned tree. Below is a simplified example where the pruned tree has only three branches. I now want to know the number of As and Bs in the three differnt branches/leafs. How can I get those?
rm(list=ls(all=TRUE))
mylabels <- matrix(nrow=1, ncol = 20)
mylabels[1,1:10] <- ("A")
mylabels[1,11:20] <- ("B")
myclusterdata <- matrix(rexp(100, rate=.1), ncol=100, nrow=20)
rownames(myclusterdata)<-mylabels
hc <- hclust(dist(myclusterdata), "ave")
memb <- cutree(hc, k = 3)
cent <- NULL
for(k in 1:3){
cent <- rbind(cent, colMeans(myclusterdata[memb == k, , drop = FALSE]))
}
hc1 <- hclust(dist(cent)^2, method = "cen", members = table(memb))
# whole tree
plot(as.dendrogram(hc),horiz=T)
# pruned tree (only 3 branches)
plot(as.dendrogram(hc1),horiz=T)