How can we calculate the Euclidean distance in R, and could you provide some examples?

Calculating the Euclidean distance in R involves using the “dist()” function, which calculates the distance between two or more points in a multidimensional space. It takes the coordinates of the points as inputs and computes the straight-line distance between them. For example, if we have two points A(1,2) and B(3,4), the Euclidean distance between them would be √((3-1)^2 + (4-2)^2) = √8 = 2.83. This function can also be used to calculate the distance between multiple points simultaneously.

Calculate Euclidean Distance in R (With Examples)


The Euclidean distance between two vectors, A and B, is calculated as:

Euclidean distance = √Σ(Ai-Bi)2

To calculate the Euclidean distance between two vectors in R, we can define the following function:

euclidean <- function(a, b) sqrt(sum((a - b)^2))

We can then use this function to find the Euclidean distance between any two vectors:

#define two vectors
a <- c(2, 6, 7, 7, 5, 13, 14, 17, 11, 8)
b <- c(3, 5, 5, 3, 7, 12, 13, 19, 22, 7)

#calculate Euclidean distance between vectors
euclidean(a, b)

[1] 12.40967

The Euclidean distance between the two vectors turns out to be 12.40967.

Note that we can also use this function to calculate the Euclidean distance between two columns of a data frame:

#define data frame
df <- data.frame(a=c(3, 4, 4, 6, 7, 14, 15),
                 b=c(4, 8, 8, 9, 14, 13, 7),
                 c=c(7, 7, 8, 5, 15, 11, 8),
                 d=c(9, 6, 6, 7, 6, 15, 19))

#calculate Euclidean distance between columns a and d
euclidean(df$a, df$d)

[1] 7.937254

Note that this function will produce a warning message if the two vectors are not of equal length:

#define two vectors of unequal length
a <- c(2, 6, 7, 7, 5, 13, 14)
b <- c(3, 5, 5, 3, 7, 12, 13, 19, 22, 7)

#attempt to calculate Euclidean distance between vectors
euclidean(a, b)

[1] 23.93742
Warning message:
In a - b : longer object length is not a multiple of shorter object length

You can refer to this Wikipedia pageto learn more details about Euclidean distance.

Additional Resources

x