How to use inbuilt dist() function In R Language
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.
How to Calculate Minkowski Distance in R?
In this article, we are going to see how to calculate Minkowski Distance in the R Programming language.