Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
R for Data Science Cookbook (n)

You're reading from  R for Data Science Cookbook (n)

Product type Book
Published in Jul 2016
Publisher
ISBN-13 9781784390815
Pages 452 pages
Edition 1st Edition
Languages
Author (1):
Yu-Wei, Chiu (David Chiu) Yu-Wei, Chiu (David Chiu)
Profile icon Yu-Wei, Chiu (David Chiu)

Table of Contents (19) Chapters

R for Data Science Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Functions in R Data Extracting, Transforming, and Loading Data Preprocessing and Preparation Data Manipulation Visualizing Data with ggplot2 Making Interactive Reports Simulation from Probability Distributions Statistical Inference in R Rule and Pattern Mining with R Time Series Mining with R Supervised Machine Learning Unsupervised Machine Learning Index

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 the cluster labels for the data:

    > fit
    
  3. Count the number of data points within each cluster:

    > table(fit)
    fit
     1  2  3 
    18 66 18 
    
  4. Make a scatter plot with fitted cluster information:

    > plot(hotel$lon, hotel$lat, col=fit)
    

    Figure 5: The...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}