O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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)
  1. Then count the number of data within each cluster:
table(fit)
  1. Finally, visualize how the data is clustered with the red rectangle border:
plot(hc)rect.hclust(hc, k = 4 , border="red"

Here is the visualization with the red rectangle border:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required