VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-calculate-euclidean-distance-in-r/

⇱ How to Calculate Euclidean Distance in R? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Calculate Euclidean Distance in R?

Last Updated : 23 Jul, 2025

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:

Output:

👁 Image

Example 2:

Output:

👁 Image

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:

Output:

👁 Image

As you can see in the output, the compiler gives us a warning since the length of vect1 is shorter than vect2.

Example 4:

Output:

👁 Image

As you can see in the output, the compiler gives us a warning since the length of vect2 is shorter than vect1.

Comment
Article Tags:

Explore