CommencerCommencer gratuitement

Calculate & plot the distance between two players

You've obtained the coordinates relative to the center of the field for two players in a soccer match and would like to calculate the distance between them.

In this exercise you will plot the positions of the 2 players and manually calculate the distance between them by using the Euclidean distance formula.

Cet exercice fait partie du cours

Cluster Analysis in R

Afficher le cours

Instructions

  • Plot their positions from the two_players data frame using ggplot.
  • Extract the positions of the players into two data frames player1 and player2.
  • Calculate the distance between player1 and player2 by using the Euclidean distance formula $$\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}$$.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Plot the positions of the players
ggplot(___, aes(x = ___, y = ___)) + 
  geom_point() +
  # Assuming a 40x60 field
  lims(x = c(-30,30), y = c(-20, 20))

# Split the players data frame into two observations
player1 <- two_players[___, ]
player2 <- two_players[___, ]

# Calculate and print their distance using the Euclidean Distance formula
player_distance <- sqrt( (player1$___ - player2$___)^2 + (player1$___ - player2$___)^2 )
player_distance
Modifier et exécuter le code