How to Calculate Manhattan Distance in R? - GeeksforGeeks

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|.
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
# Function to calculate Manhattan distance# abs() function calculate the absolute difference# between corresponding vector elements# sum() function calculates the sum of the# absolute difference between# corresponding elements of vect1 and vect2manhattanDistance <- function(vect1, vect2){     dist <- abs(vect1 - vect2)     dist <- sum(dist)     return(dist)} # Initializing a vectorvect1 <- c(3, 6, 8, 9) # Initializing another vectorvect2 <- c(1, 7, 8, 10)  print("Manhattan distance between vect1 and vect2 is: ") # Call the function to calculate Manhattan# distance between vectorsmanhattanDistance(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
# Function to calculate Manhattan distance# abs() function calculate the absolute difference# between corresponding vector elements# sum() function calculates the sum of the# absolute difference between# corresponding elements of vect1 and vect2manhattanDistance <- function(vect1, vect2){     dist <- abs(vect1 - vect2)     dist <- sum(dist)     return(dist)} # Initializing two vectors having unequal lengthvect1 <- c(14, 13, 24, 18)vect2 <- c(13, 12, 33, 11, 12) print("Manhattan distance between vect1 and vect2 is: ") # Call the function to calculate Manhattan distancemanhattanDistance(vect1, vect2)
Output:
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
# Initializing a vectorvect1 < - c(1, 16, 8, 10, 100, 20) # Initializing another vectorvect2 < - c(1, 7, 18, 90, 50, 21) # Initializing another vectorvect3 < - c(3, 10, 11, 40, 150, 210)  # Initializing another vectorvect4 < - c(2, 1, 4, 7, 8, 10)  # Initializing another vectorvect5 < - c(1, 4, 8, 3, 100, 104) # Initializing another vectorvect6 < - c(3, 7, 11, 23, 110, 114)  # Row bind vectors into a single matrixtwoDimensionalVect < - rbind(vect1, vect2, vect3, vect4, vect5, vect6) print("Manhattan distance between each pair of vectors is: ")cat("\n\n") # Calculate Manhattan distance between vectors# using built in dist method# By passing two-dimensional vector as a parameter# Since we want to calculate manhattan distance between# each unique pair of vectors# That is why we are passing manhattan as a methoddist(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
# Initializing a vectorvect1 <- c(4, 3, 5, 7, 8, 2, 10, 12) # Initializing another vectorvect2 <- c(5, 9, 4, 9, 7, 17) # Initializing another vectorvect3 <- c(3, 10, 9, 11, 13, 12)  # Initializing another vectorvect4 <- c(4, 7, 6, 12, 10, 12)  # Initializing another vectorvect5 <- c(3, 5, 12, 10, 1, 17) # Initializing another vectorvect6 <- c(4, 3, 1, 8, 7, 2)  # Using rind function to bind vectors in a 2-d vector# Note that all vectors are not of the same lengthtwoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)  print("Manhattan distance between each pair of vectors is: ")cat("\n\n") # Calculate Manhattan distance between vectors# using built in dist method# By passing two-dimensional vector as a parameter# Since we want to calculate Manhattan distance# between each pair of vectors# That is why we are passing "manhattan" as a methoddist(twoDimensionalVect, method = "manhattan")
Output:
anikakapoor
Picked
R-Statistics
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Change Color of Bars in Barchart using ggplot2 in R
How to Change Axis Scales in R Plots?
Group by function in R using Dplyr
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
How to filter R dataframe by multiple conditions?
R - if statement
How to import an Excel File into R ?
Time Series Analysis in R