Performing cluster analysis using K-means clustering

The standard R package stats provides the function for K-means clustering. We also use the cluster package to plot the results of our cluster analysis.

Getting ready

If you have not already downloaded the files for this chapter, do it now and ensure that the auto-mpg.csv file is in your R working directory. Also, ensure that you have installed the cluster package.

How to do it...

To perform cluster analysis using K-means clustering, follow theses 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) for (col ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.