O'Reilly logo

R Data Analysis Cookbook by Shanthi Viswanathan, Viswa Viswanathan

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

Performing cluster analysis using hierarchical clustering

The hclust function in the package stats helps us perform hierarchical clustering.

Getting ready

If you have not already downloaded the data files for this chapter, do it now and ensure that the auto-mpg.csv file is in R's working directory.

We will hierarchically cluster the data based on the variables mpg, cylinders, displacement, horsepower, weight, and acceleration.

How to do it...

To perform cluster analysis using hierarchical clustering, follow these steps:

  1. Read the data:
    > auto <- read.csv("auto-mpg.csv")
  2. Define a convenience function to standardize the relevant variables and append the resulting variables to the original data:
    rdacb.scale.many <- function (dat, column_nos) { nms <- names(dat) ...

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