How can the Minkowski distance be calculated in R? Can you provide examples?

Minkowski distance is a measure of dissimilarity between two points in a multidimensional space. It takes into account the differences in all dimensions between the two points, rather than just one or a few dimensions. In R, the Minkowski distance can be calculated using the “dist” function from the “stats” package. This function allows the user to specify the value of the “p” parameter, which determines the type of Minkowski distance to be calculated (e.g. Euclidean, Manhattan, Chebyshev). The function takes in a matrix or data frame as input, with each row representing a point in the space. The output is a distance matrix with the distance between each pair of points. For example, to calculate the Euclidean distance between points (1,2) and (3,4), the code would be: dist(matrix(c(1,3,2,4), ncol=2), method=”euclidian”). This would result in a distance of 2.82. Overall, the “dist” function provides a convenient and efficient way to calculate Minkowski distance in R.

Calculate Minkowski Distance in R (With Examples)


The Minkowski distance between two vectors, A and B, is calculated as:

(Σ|ai – bi|p)1/p

where i is the ith element in each vector and p is an integer.

This distance is used to measure the dissimilarity between any two vectors and is commonly used in many different machine learning algorithms.

To calculate the Minkowski distance between vectors in R, we can use the built-in dist() function with the following syntax:

dist(x, method=”minkowski”, p)

where:

  • x: A numeric matrix or data frame.
  • p: The power to use in the Minkowski distance calculation.

Note that setting p = 1 is equivalent to calculating the Manhattan distance and setting p = 2 is equivalent to calculating the Euclidean distance.

This tutorial provides a couple examples of how to use this function in practice.

Example 1: Minkowski Distance Between Two Vectors

The following code shows how to use the dist() function to calculate the Minkowski distance between two vectors in R, using a power of p = 3:

#define two vectors
a <- c(2, 4, 4, 6)
b <- c(5, 5, 7, 8)

#bind the two vectors into a single matrix
mat <- rbind(a, b)

#calculate Minkowski distance between vectors using a power of 3
dist(mat, method="minkowski", p=3)
         a
b 3.979057

The Minkowski distance (using a power of p = 3) between these two vectors turns out to be 3.979057.

Example 2: Minkowski Distance Between Vectors in a Matrix

To calculate the Minkowski distance between several vectors in a matrix, we can use similar syntax in R:

#create four vectors
a <- c(2, 4, 4, 6)

b <- c(5, 5, 7, 8)

c <- c(9, 9, 9, 8)

d <- c(1, 2, 3, 3)

#bind vectors into one matrix
mat <- rbind(a, b, c, d)

#calculate Minkowski distance between vectors using a power of 3
dist(mat, method = "minkowski", p=3)

          a         b         c
b  3.979057                    
c  8.439010  5.142563          
d  3.332222  6.542133 10.614765
  • The Minkowski distance between vector a and b is 3.98.
  • The Minkowski distance between vector a and c is 8.43.
  • The Minkowski distance between vector a and d is 3.33.
  • The Minkowski distance between vector b and c is 5.14.
  • The Minkowski distance between vector b and d is 6.54.
  • The Minkowski distance between vector c and d is 10.61.

Note that each vector in the matrix should be the same length.

Additional Resources

How to Calculate Euclidean Distance in R

How to Calculate Manhattan Distance in R
How to Calculate Mahalanobis Distance in R

x