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 = as.data.frame(scale(proteinIntake))
  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 books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.