CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Cluster Analysis in R

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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 = ___)
Modifier et exécuter le code