Cutting trees 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 the data into different groups. In this recipe, we demonstrate how to use the cutree function to separate the data into a given number of clusters.

Getting ready

In order to perform the cutree function, you need to have the previous recipe completed by generating the hclust object, hc.

How to do it...

Perform the following steps to cut the hierarchy of clusters into a given number of clusters:

  1. First, categorize the data into four groups:
    > fit = cutree(hc, k = 4)
    
  2. You can then examine ...

Get R: Recipes for Analysis, Visualization and Machine Learning 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.