How to Calculate Minkowski Distance in R?
Last Updated :
14 Jan, 2022
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 is widely used in the field of Machine learning, especially in the concept to find the optimal correlation or classification of data. Minkowski distance is used in certain algorithms also like K-Nearest Neighbors, Learning Vector Quantization (LVQ), Self-Organizing Map (SOM), and K-Means Clustering.
Let us consider a 2-dimensional space having three points P1 (X1, Y1), P2 (X2, Y2), and P3 (X3, Y3), the Minkowski distance is given by ( |X1 - Y1|p + |X2 - Y2|p + |X2 - Y2|p )1/p. In R, Minkowski distance is calculated with respect to vectors.
For example,
we are given two vectors, vect1 as (4, 2, 6, 8) and vect2 as (5, 1, 7, 9). Their Minkowski distance for p = 2 is given by, ( |4 - 5|2 + |2 - 1|2 + |6 - 7|2 + |8 - 9|2 )1/2 which is equal to 2. This article focuses upon how we can calculate Minkowski distance in R.
Method 1:Using a custom function
We can calculate Minkowski distance between a pair of vectors by apply the formula,
( Σ|vector1i - vector2i|p )1/p
Here,
vector1 is the first vector
vector2 is the second vector
p is an integer
Below is the implementation in R to calculate Minkowski distance by using a custom function.
R
# R program to illustrate how to
# calculate Minkowski distance
# using a custom function
# Custom function to calculate Minkowski distance
calculateMinkowskiDistance <- function(vect1, vect2, p) {
# Initializing answer variable as 0
answer <- as.integer(0)
# Iterating over the length of the vector
# Using for-in loop
for (index in 0 : length(vect1))
{
# temp stores the absolute difference raised to power p
temp = as.integer(abs(vect1[index] - vect2[index]) ^ p)
# Updating answer variable
answer = sum(temp, answer)
}
# The final answer would be answer raised to
# power 1 / p
answer = answer ^ (1 / p)
# Return the answer
return(answer)
}
# Initializing a vector
vect1 <- c(1, 3, 5, 7)
# Initializing another vector
vect2 <- c(2, 4, 6, 8)
# Set p equal to 4
p <- as.integer(1)
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
# Set p equal to 5
p <- as.integer(2)
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
# Set p equal to 5
p <- as.integer(3)
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
# Set p equal to 5
p <- as.integer(4)
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
# Print the calculated distance
print(paste("The Minkowski distance between vect1 \
and vect2 having the value of p =",p, "is", distance ))
Output:

Method 2: Using inbuilt dist() function
R provides inbuilt dist function using which we can calculate six types of distances including Minkowski distance. This function accepts a two-dimensional vector or a matrix as a parameter. This function is quite useful as it calculates the Minkowski distance between each unique pair of vectors specified in a two-dimensional vector.
Syntax: dist(vect, method = "minkowski", p = integer, diag = TRUE or FALSE, upper = TRUE or FALSE)
Parameters:
- vect: A two-dimensional vector
- method: It must be equal to "minkowski"
- p: It must be equal to an integer
- diag: logical value (TRUE or FALSE) that conveys whether the diagonal of the distance matrix should be printed by print.dist or not.
- upper: logical value (TRUE or FALSE) that conveys whether the upper triangle of the distance matrix should be printed by print.dist or not.
Return type:
It return an object of class "dist" which represents Minkowski distance between each unique pair of rows or vectors.
Note: diag and upper parameters are optional
Example 1: Implementation using vectors of equal length.
R
# R program to illustrate how to calculate
# Minkowski distance By using inbuilt dist()
# function
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
vect4, vect5, vect6)
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
# Calculate Minkowski distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski distance
# between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
diag = TRUE, upper = TRUE p = 2)
Output:
Note that the length of all vectors present in a two-dimensional vector has to be the same. Otherwise, the compiler will produce a warning message.
Example 2: Implementation using vectors of unequal length.
R
# R program to illustrate
# how to calculate Minkowski distance
# By using inbuilt dist() function
# Initializing a vector
# Note that the length of vec1 is one
# more than the other vectors
vect1 <- c(2, 4, 1, 9, 2, 3, 10)
# Initializing another vector
vect2 <- c(4, 8, 1, 2, 4, 7)
# Initializing another vector
vect3 <- c(11, 7, 9, 3, 2, 8)
# Initializing another vector
vect4 <- c(21, 1, 4, 7, 8, 9)
# Initializing another vector
vect5 <- c(11, 4, 8, 3, 9, 21)
# Initializing another vector
vect6 <- c(6, 7, 8, 6, 5, 9)
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2,
vect3, vect4,
vect5, vect6)
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
# Calculate Minkowski distance between
# vectors using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski
# distance between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
diag = TRUE, upper = TRUE p = 2)
Output:
As you can in the output, the compiler produces a warning message when vectors are of unequal lengths.
Similar Reads
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
How to Calculate Levenshtein Distance in R? In this article, we will discuss how to calculate Levenshtein Distance in the R Programming Language. The Levenshtein distance between two strings is the minimum number of character substitutions, insertions, and deletions required to turn one string into the other string. The Levenshtein distance
3 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 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 Create a Distance Matrix in R? A distance matrix is a matrix that contains the distance between each pair of elements in a dataset. In R Programming Language, there are several functions available for creating a distance matrix such as dist(), daisy(), and vegdist() from the stats, cluster, and vegan packages respectively. Distan
8 min read