Simple k-means cluster analysis and plot

Average: 4 (1 vote)

We know that the iris data set include three species. K-means does a pretty good job of correctly identifying correct groups.

data(iris)
cl <- kmeans(iris[,1:4], 3)
cl$cluster
cbind(1:150,iris$Species)

cl$cluster reveals which group each observation was placed in; compare this with a listing of the species designations mapped next to 150 index values (cbind(1:150,iris$Species)).

If you would like to plot each cluster, you can set plot's "col" to the cl$cluster vector, coloring each observation with the integer based on the cluster analysis.

plot(iris[,1:4], col = cl$cluster)