How to calculate Euclidean Distance in R (With Examples)

Euclidean distance in R is a measure of the straight-line distance between two points in a multidimensional space. It can be calculated using the dist() function in R, which takes two vectors as its arguments and returns the Euclidean distance between them. Examples of this are provided in the form of code, which demonstrate how to calculate the distance between two points, or between two matrices. Additionally, this code also provides examples of how to calculate the distance between two data frames.


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 page to learn more details about Euclidean distance.

x