Cutting tree into clusters
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 data into different groups. In this recipe, we demonstrate how to use the cutree
function to separate data into a given number of clusters.
Getting ready
In order to perform the cutree
function, one needs to have completed the previous recipe by generating an hclust object, hc
.
How to do it…
Please perform the following steps to cut the hierarchy of clusters into a given number of clusters:
- First, categorize the data into three groups:
> fit <- cutree(hc, k = 3)
- You can then examine the cluster labels for the data:
> fit
- Count the number of data points within each cluster:
> table(fit) fit 1 2 3 18 66 18
- Make a scatter plot with fitted cluster information:
> plot(hotel$lon, hotel$lat, col=fit)