How to Calculate Manhattan Distance in R

Spread the love

Manhattan distance, also known as city block distance, taxicab distance, or simply L1 distance, is a distance metric between two points in a N dimensional vector space. It is the sum of the lengths of the projections of the line segment between the points onto the coordinate axes.

In many practical scenarios, such as in recommendation systems, text mining, and other machine learning applications, measuring the difference between data points is crucial. One such metric is the Manhattan distance, named after the grid layout of the Manhattan streets in New York.

In this comprehensive guide, we’ll take a deep dive into calculating Manhattan distance using R.

Understanding Manhattan Distance

The Manhattan distance metric measures the distance between two points in a grid-based system (like a taxi moving around on the streets). In more technical terms, given two points in a plane p1 = (x1, y1) and p2 = (x2, y2), the Manhattan distance between these points is given by:

| x1 – x2 | + | y1 – y2 |

It’s important to note that this metric treats each dimension independently, which means it can behave differently from other distance metrics, like Euclidean distance, in higher dimensional spaces.

Calculating Manhattan Distance Using Base R

To compute Manhattan distance in base R, we can use the dist function with method = "manhattan". The dist function computes the distance matrix, which can be applied to a data frame where each row is considered a vector.

For instance, consider two vectors A and B, where A = (1, 2) and B = (3, 6). Here is how we can compute the Manhattan distance:

# Define the vectors
A <- c(1, 2)
B <- c(3, 6)

# Combine vectors into a matrix
data <- rbind(A, B)

# Calculate Manhattan distance
manhattan_distance <- dist(data, method = "manhattan")

In the above code, the vectors are first combined into a data frame using the rbind function. The dist function is then used to compute the Manhattan distance.

Using the proxy Package

The ‘proxy’ package in R provides a flexible framework for the computation of the distance (and similarity) between structured data types and includes a large variety of distance measures. To compute the Manhattan distance, you can use the dist function with method = "Manhattan".

First, install and load the ‘proxy’ package, then calculate the Manhattan distance:

# Install the proxy package
if (!"proxy" %in% rownames(installed.packages())) {install.packages("proxy")}

# Load the proxy package

# Define the vectors
A <- c(1, 2)
B <- c(3, 6)

# Calculate Manhattan distance
manhattan_distance <- proxy::dist(A, B, method = "Manhattan")

In this example, the dist function from the proxy package calculates the Manhattan distance between the vectors A and B.


Manhattan distance, also known as City Block distance, is a useful metric that calculates the distance between two points in a more grid-like pattern. In this guide, we went over the process of calculating Manhattan distance in R, using both base R functionality and the dedicated ‘proxy’ package.

Posted in RTagged

Leave a Reply