In a dendrogram, we can see the hierarchy of clusters, but we have not grouped data into different clusters yet. However, we can determine how many clusters are within the dendrogram and cut the dendrogram at a certain tree height to separate the data into different groups. In this recipe, we will demonstrate how to use the cutree
function to separate the data into a given number of clusters.
In order to perform the cutree
function, you need to have the previous recipe completed by generating the hclust
object, hc
.
Perform the following steps to cut the hierarchy of clusters into a given number of clusters:
- First, categorize the data into four groups:
> fit = cutree(hc, k = 4)
- You can then examine the cluster labels for the data:
> fit Output [1] 1 1 2 1 2 1 2 2 1 1 1 2 2 1 1 1 2 1 2 3 4 3 4 3 3 4 4 3 4 [30] 4 4 3 3 3 4 4 3 4 4 4 4 4 4 4 3 3 4 4 4 3 4 3 3 4 4 4 3 4 [59] 4 3
- Count the...