Get startedGet started for free

Using the dist() function

Using the Euclidean formula manually may be practical for 2 observations but can get more complicated rather quickly when measuring the distance between many observations.

The dist() function simplifies this process by calculating distances between our observations (rows) using their features (columns). In this case the observations are the player positions and the dimensions are their x and y coordinates.

Note: The default distance calculation for the dist() function is Euclidean distance

This exercise is part of the course

Cluster Analysis in R

View Course

Exercise instructions

  • Calculate the distance between two players using the dist() function for the data frame two_players.
  • Calculate the distance between three players for the data frame three_players.

Hands-on interactive exercise

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

# Calculate the Distance Between two_players
dist_two_players <- ___
dist_two_players

# Calculate the Distance Between three_players
dist_three_players <- ___
dist_three_players
Edit and Run Code