Open In App

How to Calculate Manhattan Distance in R?

Last Updated : 24 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




# 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 vect2
manhattanDistance <- function(vect1, vect2){
     dist <- abs(vect1 - vect2)
     dist <- sum(dist)
     return(dist)
}
 
# Initializing a vector
vect1 <- c(3, 6, 8, 9)
 
# Initializing another vector
vect2 <- c(1, 7, 8, 10)
 
 
print("Manhattan distance between vect1 and vect2 is: ")
 
# Call the function to calculate Manhattan
# distance between vectors
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




# 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 vect2
manhattanDistance <- function(vect1, vect2){
     dist <- abs(vect1 - vect2)
     dist <- sum(dist)
     return(dist)
}
 
# Initializing two vectors having unequal length
vect1 <- 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 distance
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




# Initializing a vector
vect1 < - c(1, 16, 8, 10, 100, 20)
 
# Initializing another vector
vect2 < - c(1, 7, 18, 90, 50, 21)
 
# Initializing another vector
vect3 < - c(3, 10, 11, 40, 150, 210)
 
 
# Initializing another vector
vect4 < - c(2, 1, 4, 7, 8, 10)
 
 
# Initializing another vector
vect5 < - c(1, 4, 8, 3, 100, 104)
 
# Initializing another vector
vect6 < - c(3, 7, 11, 23, 110, 114)
 
 
# Row bind vectors into a single matrix
twoDimensionalVect < - 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 method
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




# Initializing a vector
vect1 <- c(4, 3, 5, 7, 8, 2, 10, 12)
 
# Initializing another vector
vect2 <- c(5, 9, 4, 9, 7, 17)
 
# Initializing another vector
vect3 <- c(3, 10, 9, 11, 13, 12)
 
 
# Initializing another vector
vect4 <- c(4, 7, 6, 12, 10, 12)
 
 
# Initializing another vector
vect5 <- c(3, 5, 12, 10, 1, 17)
 
# Initializing another vector
vect6 <- 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 length
twoDimensionalVect <- 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 method
dist(twoDimensionalVect, method = "manhattan")


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads