manhattan {abdiv}R Documentation

Manhattan and related distances


The Manhattan or city block distance is the sum of absolute differences between the elements of two vectors. The mean character difference is a closely related measure.


manhattan(x, y)

mean_character_difference(x, y)

modified_mean_character_difference(x, y)


x, y

Numeric vectors


For vectors x and y, the Manhattan distance is given by

d(x, y) = ∑_i |x_i - y_i|.

Relation of manhattan() to other definitions:

The mean character difference is the Manhattan distance divided by the length of the vectors. It was proposed by Cain and Harrison in 1958. Relation of mean_character_difference() to other definitions:

The modified mean character difference is the Manhattan distance divided by the number elements where either x or y (or both) are nonzero. Relation of modified_mean_character_difference() to other definitions:


The distance between x and y. The modified mean character difference is undefined if all elements in x and y are zero, in which case we return NaN.


Cain AJ, Harrison GA. An analysis of the taxonomist's judgment of affinity. Proceedings of the Zoological Society of London 1958;131:85-98.


x <- c(15, 6, 4, 0, 3, 0)
y <- c(10, 2, 0, 1, 1, 0)
manhattan(x, y)
# Whittaker's index of association
manhattan(x / sum(x), y / sum(y)) / 2

mean_character_difference(x, y)
# Simple matching coefficient for presence/absence data
# Should be 2 / 6
mean_character_difference(x > 0, y > 0)

modified_mean_character_difference(x, y)
# Jaccard distance for presence/absence data
modified_mean_character_difference(x > 0, y > 0)
jaccard(x, y)

[Package abdiv version 0.2.0 Index]