hc <- hclust(dist(USArrests[c(1, 6, 13, 20, 23), ]), "ave")
dend <- as.dendrogram(hc)
cutree(hc, k = 3) # on hclust
cutree_1k.dendrogram(dend, k = 3) # on a dendrogram
labels(dend)
# the default (ordered by original data's order)
cutree_1k.dendrogram(dend, k = 3, order_clusters_as_data = TRUE)
# A different order of labels - order by their order in the tree
cutree_1k.dendrogram(dend, k = 3, order_clusters_as_data = FALSE)
# make it faster
if (FALSE) {
library(microbenchmark)
dend_ks <- heights_per_k.dendrogram
microbenchmark(
cutree_1k.dendrogram = cutree_1k.dendrogram(dend, k = 4),
cutree_1k.dendrogram_no_labels = cutree_1k.dendrogram(dend,
k = 4, use_labels_not_values = FALSE
),
cutree_1k.dendrogram_no_labels_per_k = cutree_1k.dendrogram(dend,
k = 4, use_labels_not_values = FALSE,
dend_heights_per_k = dend_ks
)
)
# the last one is the fastest...
}
Run the code above in your browser using DataLab