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:

  1. First, categorize the data into three groups:
    > fit <- cutree(hc, k = 3)
    
  2. You can then examine ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.