distance: Distance Between Two Vectors

View source: R/distance.R

distanceR Documentation

Distance Between Two Vectors

Description

Computes Manhattan, Euclidean, Chi, or Hellinger distances between numeric vectors of the same length. Used internally within [distanceMatrix()] and [autoSum()].

Usage

distance(x, y, method = c("manhattan", "chi", "hellinger", "euclidean"))

Arguments

x

(required, numeric vector).

y

(required, numeric vector) of same length as 'x'.

method

(required, character string) name of a distance metric. Valid entries are: "manhattan", "euclidean", "chi", and "hellinger".

Details

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

  • "manhattan": 'sum(abs(x - y))'

  • "euclidean": 'qrt(sum((x - y)^2))'.

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

  • "hellinger": 'sqrt(1/2 * sum((sqrt(x) - sqrt(y))^2))'

Note that zeroes are replaced by 'mean(c(x, y)) * 0.001' whem 'method' is "chi" or "hellinger".

Value

A distance value.

Author(s)

Blas Benito <blasbenito@gmail.com>

Examples


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


BlasBenito/distantia documentation built on Nov. 17, 2023, 11:06 p.m.