Exercise

K-means on a soccer field (part 2)

In the previous exercise, you successfully used the k-means algorithm to cluster the two teams from the lineup data frame. This time, let's explore what happens when you use a k of 3.

You will see that the algorithm will still run, but does it actually make sense in this context…

Instructions

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