How to Calculate Minkowski Distance in R? - GeeksforGeeks

14 Jan, 2022
In this article, we are going to see how to calculate Minkowski Distance in the R Programming language.
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.
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 vectorvect1 <- c(1, 3, 5, 7) # Initializing another vectorvect2 <- c(2, 4, 6, 8) # Set p equal to 4p <- as.integer(1) # Call the function to calculate MinkowskiDistancedistance = calculateMinkowskiDistance(vect1, vect2, p) # Print the calculated distanceprint(paste("The Minkowski distance between vect1\and vect2 having the value of p =",p, "is", distance )) # Set p equal to 5p <- as.integer(2) # Call the function to calculate MinkowskiDistancedistance = calculateMinkowskiDistance(vect1, vect2, p) # Print the calculated distanceprint(paste("The Minkowski distance between vect1\and vect2 having the value of p =",p, "is", distance )) # Set p equal to 5p <- as.integer(3) # Call the function to calculate MinkowskiDistancedistance = calculateMinkowskiDistance(vect1, vect2, p) # Print the calculated distanceprint(paste("The Minkowski distance between vect1\and vect2 having the value of p =",p, "is", distance )) # Set p equal to 5p <- as.integer(4) # Call the function to calculate MinkowskiDistancedistance = calculateMinkowskiDistance(vect1, vect2, p) # Print the calculated distanceprint(paste("The Minkowski distance between vect1 \and vect2 having the value of p =",p, "is", distance ))
Output:
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 vectorvect1 <- c(1, 4, 8, 9, 2, 3) # Initializing another vectorvect2 <- c(9, 4, 1, 2, 4, 7) # Initializing another vectorvect3 <- c(1, 7, 9, 3, 2, 8) # Initializing another vectorvect4 <- c(2, 1, 4, 7, 8, 9) # Initializing another vectorvect5 <- c(1, 4, 8, 3, 9, 2) # Initializing another vectorvect6 <- c(3, 7, 8, 6, 5, 9) #Row bind vectors into a single matrixtwoDimensionalVect <- 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 methoddist(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 vectorsvect1 <- c(2, 4, 1, 9, 2, 3, 10) # Initializing another vectorvect2 <- c(4, 8, 1, 2, 4, 7) # Initializing another vectorvect3 <- c(11, 7, 9, 3, 2, 8) # Initializing another vectorvect4 <- c(21, 1, 4, 7, 8, 9) # Initializing another vectorvect5 <- c(11, 4, 8, 3, 9, 21) # Initializing another vectorvect6 <- c(6, 7, 8, 6, 5, 9) #Row bind vectors into a single matrixtwoDimensionalVect <- 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 methoddist(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.
sweetyty
arorakashish0911
simranarora5sos
bhuwanesh
Picked
R-Statistics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to Change Axis Scales in R Plots?
Replace Specific Characters in String in R
R - if statement
How to filter R dataframe by multiple conditions?
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
Time Series Analysis in R