How to Calculate Manhattan Distance in R?
Last Updated :
24 Dec, 2021
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 distance is given by |x1 – x2| + |y1 – y2|.
Method 1: Using Formulae Approach
In R Manhattan distance is calculated with respect to vectors. The Manhattan distance between the two vectors is given by,
Σ|vect1i - vect2i|
where,
- vect1 is the first vector
- vect2 is the second vector
For example, we are given two vectors, vect1 as (3, 6, 8, 9) and vect2 as (1, 7, 8, 10). Their Manhattan distance is given by, |3 – 1| + |6 – 7| + |8 – 8| + |9 – 10| which is equal to 4.
Below is the implementation using two vectors of equal length:
Example 1:
R
manhattanDistance <- function (vect1, vect2){
dist <- abs (vect1 - vect2)
dist <- sum (dist)
return (dist)
}
vect1 <- c (3, 6, 8, 9)
vect2 <- c (1, 7, 8, 10)
print ( "Manhattan distance between vect1 and vect2 is: " )
manhattanDistance (vect1, vect2)
|
Output:
[1] "Manhattan distance between vect1 and vect2 is: "
[1] 4
Example 2:
If the two vectors have unequal length then the compiler gives a warning message. Below is the implementation using two vectors having unequal lengths.
R
manhattanDistance <- function (vect1, vect2){
dist <- abs (vect1 - vect2)
dist <- sum (dist)
return (dist)
}
vect1 <- c (14, 13, 24, 18)
vect2 <- c (13, 12, 33, 11, 12)
print ( "Manhattan distance between vect1 and vect2 is: " )
manhattanDistance (vect1, vect2)
|
Output:
Method 2: Using dist() function
R provides an inbuilt function using which we can find the Manhattan distance between each unique pair of vectors in a 2-dimensional vector.
Syntax:
dist(2dVect, method = “manhattan”)
Parameters:
- 2dVect: two-dimensional vector
- method: the distance measure to be used. This can be one of “euclidean”, “maximum”, “manhattan”, “canberra”, “binary”
Return type:
It return an object of class “dist”
Example 1:
Below is the implementation to find Manhattan distance using dist() function:
R
vect1 < - c (1, 16, 8, 10, 100, 20)
vect2 < - c (1, 7, 18, 90, 50, 21)
vect3 < - c (3, 10, 11, 40, 150, 210)
vect4 < - c (2, 1, 4, 7, 8, 10)
vect5 < - c (1, 4, 8, 3, 100, 104)
vect6 < - c (3, 7, 11, 23, 110, 114)
twoDimensionalVect < - rbind (vect1, vect2, vect3, vect4, vect5, vect6)
print ( "Manhattan distance between each pair of vectors is: " )
cat ( "\n\n" )
dist (twoDimensionalVect, method= "manhattan" )
|
Output:
Example 2:
Note that the length of all the vectors presented under the 2-dimensional vector is required to be the same otherwise, the R compiler produces a compiler-time error.
R
vect1 <- c (4, 3, 5, 7, 8, 2, 10, 12)
vect2 <- c (5, 9, 4, 9, 7, 17)
vect3 <- c (3, 10, 9, 11, 13, 12)
vect4 <- c (4, 7, 6, 12, 10, 12)
vect5 <- c (3, 5, 12, 10, 1, 17)
vect6 <- c (4, 3, 1, 8, 7, 2)
twoDimensionalVect <- rbind (vect1, vect2, vect3, vect4, vect5, vect6)
print ( "Manhattan distance between each pair of vectors is: " )
cat ( "\n\n" )
dist (twoDimensionalVect, method = "manhattan" )
|
Output:
Share your thoughts in the comments
Please Login to comment...