1. Learn
  2. /
  3. Courses
  4. /
  5. Cluster Analysis in R

Connected

Exercise

K-means on a soccer field

In the previous chapter, you used the lineup dataset to learn about hierarchical clustering, in this chapter you will use the same data to learn about k-means clustering. As a reminder, the lineup data frame contains the positions of 12 players at the start of a 6v6 soccer match.

Just like before, you know that this match has two teams on the field so you can perform a k-means analysis using k = 2 in order to determine which player belongs to which team.

Note that in the kmeans() function k is specified using the centers parameter.

Instructions

100 XP
  • Build a k-means model called model_km2 for the lineup data using the kmeans() function with centers = 2.
  • Extract the vector of cluster assignments from the model model_km2$cluster and store this in the variable clust_km2.
  • Append the cluster assignments as a column cluster to the lineup data frame and save the results to a new data frame called lineup_km2.
  • Use ggplot to plot the positions of each player on the field and color them by their cluster.