Get startedGet started for free

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.

This exercise is part of the course

Cluster Analysis in R

View Course

Exercise 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}$$.

Hands-on interactive exercise

Have a go at this exercise by completing this sample 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
Edit and Run Code