How to do it...

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

  1. First, load the protein.csv file and do some preprocessing to add row names as Country name and remove the Country variable before normalizing the data:

> proteinIntake <- read.csv("protein.csv")> rownames(proteinIntake)=proteinIntake$Country> proteinIntake$Country=NULL> proteinIntakeScaled =
  1. Now use kmeans to cluster the scaled protein intake data:
> set.seed(22) ## To fix the random cluster initialization> kmFit = kmeans(proteinIntakeScaled, 4)> kmFit

Here is the K-means clustering indicative of the code:

Get R Data Analysis Cookbook - Second Edition now with the O’Reilly learning platform.

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