How to Calculate Euclidean Distance in R?
Last Updated :
30 Jul, 2024
Euclidean distance between two points in Euclidean space is the length of a line segment between the two points. It can be calculated from the Cartesian coordinates of the points using the Pythagorean theorem, therefore occasionally being called the Pythagorean distance. The Euclidean distance between the two vectors is given by
√Σ(vect1i - vect2i)2
where,
- vect1 is the first vector
- vect2 is the second vector
For example, we are given two vectors, vect1 as (1, 4, 3, 5) and vect2 as (2, 3, 2, 4). Their Euclidean distance is given by, √(1 - 2)2 + (4 - 3)2 + (3 - 2)2 + (5 - 4)2 which is equal to 2. Below is the implementation using two vectors of equal length:
Example 1:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(2, 4, 4, 7)
vect2 <- c(1, 2, 2, 10)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
Output:

Example 2:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(2, 3, 4, 7)
vect2 <- c(1, 2, 3, 8)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
Output:

If the two vectors have unequal length then the compiler gives a warning message. Below is the implementation using two vectors having unequal length.
Example 3:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(4, 3, 4, 8)
vect2 <- c(3, 2, 3, 1, 2)
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
Output:

As you can see in the output, the compiler gives us a warning since the length of vect1 is shorter than vect2.
Example 4:
R
# Function to calculate Euclidean distance
# Sum function calculates the sum of the
# squares of absolute difference between
# corresponding elements of vect1 and vect2
CalculateEuclideanDistance <- function(vect1, vect2) sqrt(sum((vect1 - vect2)^2))
# Initializing two vectors having equal length
vect1 <- c(1, 7, 1, 3, 10, 15 )
vect2 <- c(3, 2, 10, 11 )
print("Euclidean distance between vect1 and vect2 is: ")
# Calling CalculateEuclideanDistance function
CalculateEuclideanDistance(vect1, vect2)
Output:

As you can see in the output, the compiler gives us a warning since the length of vect2 is shorter than vect1.
Similar Reads
How to Calculate Euclidean Distance in Excel? Euclidean distance is the distance between two real-valued vectors. It is calculated by the square root of the sum of the squared differences of the elements in the two vectors.The formula to calculate Euclidean distance is :In this article we are going to discuss how to calculate the Euclidean dist
2 min read
How to Calculate Hamming Distance in R? In this article, we will be looking at various methods to calculate the hamming distance in the R programming language.Hamming distance between two data collections is the number of positions at which corresponding elements are different. In other words, we can say that the minimum number of changes
8 min read
How to Calculate Distance Distance is a fundamental concept in mathematics and physics, representing the extent of space between two points, lines, or planes. It's a crucial metric used in various fields, including navigation, physics, engineering, and everyday life. Calculating distance accurately is essential for solving p
6 min read
How to Calculate Minkowski Distance in R? In this article, we are going to see how to calculate Minkowski Distance in the R Programming language. Minkowski distance:Â Minkowski distance is a distance measured between two points in N-dimensional space. It is basically a generalization of the Euclidean distance and the Manhattan distance. It
6 min read
How to Calculate Manhattan Distance in R? Manhattan distance is a distance metric between two points in an N-dimensional vector space. It is defined as the sum of absolute distance between coordinates in corresponding dimensions. For example, In a 2-dimensional space having two points Point1 (x1,y1) and Point2 (x2,y2), the Manhattan distan
4 min read
How to Calculate Mahalanobis Distance in R? In this article, we are going to calculate Mahalanobis distance in R Programming Language. Mahalanobis distance is used to calculate the distance between two points or vectors in a multivariate distance metric space which is a statistical analysis involving several variables. To start with we need a
3 min read