How to Calculate Euclidean Norm of a Vector in R


In statistics, one of the most common ways to calculate the length of a vector is to calculate the Euclidean norm.

This is defined as the square root of the sum of the squares of the elements in the vector.

Written in mathematical terms, we would say:

Euclidean norm = Σxi2

The easiest way to calculate this value in the R programming language is by using the norm() function from base R with the following syntax:

norm(x, type=”2″)

where:

  • x: The name of the vector to calculate the Euclidean norm of

The following examples show how to use this function in practice to calculate the Euclidean norm of different vectors.

Note: The norm() function is built-in with base R so you don’t need to install or load any external packages to use this function.

Example: How to Calculate Euclidean Norm in R

Suppose we have a vector named my_vector with only two elements:

my_vector <- c(4, 9)

Suppose that we would like to calculate the length of this vector using the formula for the Euclidean norm.

We can use the norm() function with the following syntax to do so:

#calculate Euclidean norm of elements in my_vector
norm(my_vector, type="2")

[1] 9.848858

This returns a value of 9.848858.

We can confirm that this is correct by manually calculating the Euclidean norm of this vector:

  • Euclidean norm: √Σxi2
  • Euclidean norm: √(42+92
  • Euclidean norm: √(16+81)
  • Euclidean norm: √(97
  • Euclidean norm: 9.848858

This matches the value calculated by the norm() function in R.

Note that we can use the norm() function to calculate the Euclidean norm for a vector with any number of elements that we would like.

For example, we could use the norm() function to calculate the Euclidean norm for the following vector with four elements:

#define vector with four elements
my_vector <- c(4, 9, 5, 6)

#calculate Euclidean norm of elements in my_vector
norm(my_vector, type="2")

[1] 12.56981

This returns a value of 12.56981.

Note that we could also write a custom function to easily calculate the Euclidean norm of a vector as well if we don’t want to use the norm() function from base R.

We can use the following syntax to do so:

#create custom function to calculate the Eucliden norm of a vector
euclid_norm <- function(x) sqrt(sum(x^2))

We chose to name this function euclid_norm but you can choose whatever name you would like when replicating this function.

Suppose that we use this function to calculate the Euclidean norm of the same vector used in the previous example:

#define vector with four elements
my_vector <- c(4, 9, 5, 6)

#calculate Euclidean norm of elements in my_vector
euclid_norm(my_vector)

[1] 12.56981

This returns a value of 12.56981. This matches the value calculated by the norm() function from base R.

The only difference with this function is that we only had to provide one argument – the name of the vector.

Feel free to use whichever function you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Calculate Manhattan Distance in R
How to Calculate Minkowski Distance in R
How to Calculate Hamming Distance in R
How to Calculate Mahalanobis Distance in R
How to Calculate Levenshtein Distance in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *