# How to 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)

 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)

 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)

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