Get startedGet started for free

Assign cluster membership

In this exercise you will leverage the hclust() function to calculate the iterative linkage steps and you will use the cutree() function to extract the cluster assignments for the desired number (k) of clusters.

You are given the positions of 12 players at the start of a 6v6 soccer match. This is stored in the lineup data frame.

You know that this match has two teams (k = 2), let's use the clustering methods you learned to assign which team each player belongs in based on their position.

Notes:

  • The linkage method can be passed via the method parameter: hclust(distance_matrix, method = "complete")
  • Remember that in soccer opposing teams start on their half of the field.
  • Because these positions are measured using the same scale we do not need to re-scale our data.

This exercise is part of the course

Cluster Analysis in R

View Course

Exercise instructions

  • Calculate the Euclidean distance matrix dist_players among all twelve players.
  • Perform the complete linkage calculation for hierarchical clustering using hclust and store this as hc_players.
  • Build the cluster assignment vector clusters_k2 using cutree() with a k = 2.
  • Append the cluster assignments as a column cluster to the lineup data frame and save the results to a new data frame called lineup_k2_complete.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate the Distance
dist_players <- ___

# Perform the hierarchical clustering using the complete linkage
hc_players <- ___

# Calculate the assignment vector with a k of 2
clusters_k2 <- ___

# Create a new data frame storing these results
lineup_k2_complete <- mutate(lineup, cluster = ___)
Edit and Run Code