R Tutorial: Distance between two observations

Learn to measure distance between observations using Euclidean distance. Two soccer players at coordinates (0,0) and (9,12) are 15 units apart. Use R's dist() function to calculate pairwise distances for clustering.
Chapters
Let's begin by focusing on the question that is fundamental to all clustering analysis. How similar are two observations? Or from another perspective, how dissimilar are they? You see, most clustering methods measure similarity between observations using a dissimilarity metric, often referred to as a distance. These two concepts are just two sides of the same coin. If two observations have a large distance, then they are less similar to one another. Likewise, if their distance value is small, then they are more similar. Naturally, we should first develop a keen intuition about what is meant by distance. So let's work with a scenario of players on a soccer field. In this image, you see the position of two players. How far apart are they? To answer this question, we first need their coordinates. Here, the blue player is positioned in the center of the field, which we will refer to AS00, while the red player has a position of 12 and 9 or 12ft to the right of center and 9ft up. The players in this case are our observations and their x and Y coordinates are the features of these observations. We can use these features to calculate the distance between the two players. In this case, we will use a distance measurement. You are likely familiar with the Euclidean distance, which is simply the hypotenuse of the triangle that is formed by the differences in the x and Y coordinates of these players. The familiar formula to calculate this is shown here, which if we plug in our values of x and Y for both players, we arrive at the Euclidean distance between them, which in this case is 15. This is the fundamental idea for calculating a measure of dissimilarity between the blue and red players. To do this in R, we use the DIST function to calculate the Euclidean distances between our observations. The function simply requires a data frame or matrix containing our observations and features. In this case, we are working with the DataFrame 2 players. The method by which the distance is calculated is provided by the method parameter. In this case, we are using the Euclidean distance and specify it accordingly, as in our manual calculation, we see that the distance between the red and blue players is 15. This function becomes indispensable if we have more than two observations. In this example, if we wanted to know the distance between three players, we would measure the distance between the players two at a time. Running this through the DIST function, we see that the distance between players red and blue is 15 as before. But we also have measurements between green and blue as well as green and red. In this case, green and red have the smallest distance and hence are closest to one another. The dist function would work just as well if we have more features to use for calculating the distance. Now, let's put what you've just learned into practice with some upcoming exercises.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







