Computes a multivariate distance (one of: "manhattan", "euclidean", "chi", and "hellinger") between two vectors of the same length. It is used internally by distanceMatrix and autoSum. This function has no buit-in error trapping procedures in order to speed up execution.

distance(x, y, method = "manhattan")

Arguments

x

numeric vector.

y

numeric vector of the same length as x.

method

character string naming a distance metric. Valid entries are: "manhattan", "euclidean", "chi", and "hellinger". Invalid entries will throw an error.

Value

A number representing the distance between both vectors.

Details

Vectors x and y are not checked to speed-up execution time. Distances are computed as:

  • manhattan: d <- sum(abs(x - y))

  • euclidean: d <- sqrt(sum((x - y)^2))

  • chi: xy <- x + y y. <- y / sum(y) x. <- x / sum(x) d <- sqrt(sum(((x. - y.)^2) / (xy / sum(xy))))

  • hellinger: d <- sqrt(1/2 * sum(sqrt(x) - sqrt(y))^2)

Note that zeroes are replaced by 0.00001 whem method equals "chi" or "hellinger".

Examples

x <- runif(100) y <- runif(100) distance(x, y, method = "manhattan")
#> [1] 32.83516