fdist | R Documentation |
A fast and flexible replacement for dist
, to compute euclidean distances.
fdist(x, v = NULL, ..., method = "euclidean", nthreads = .op[["nthreads"]])
x |
a numeric vector or matrix. Data frames/lists can be passed but will be converted to matrix using | ||||||||||||||||
v |
an (optional) numeric (double) vector such that | ||||||||||||||||
... |
not used. A placeholder for possible future arguments. | ||||||||||||||||
method |
an integer or character string indicating the method of computing distances.
| ||||||||||||||||
nthreads |
integer. The number of threads to use. If |
If v = NULL
, a full lower-triangular distance matrix between the rows of x
is computed and returned as a 'dist' object (all methods apply, see dist
). Otherwise, a numeric vector of distances of each row of x
with v
is returned. See Examples.
fdist
does not check for missing values, so NA
's will result in NA
distances.
kit::topn
is a suitable complimentary function to find nearest neighbors. It is very efficient and skips missing values by default.
flm
, Fast Statistical Functions, Collapse Overview
# Distance matrix
m = as.matrix(mtcars)
str(fdist(m)) # Same as dist(m)
# Distance with vector
d = fdist(m, fmean(m))
kit::topn(d, 5) # Index of 5 nearest neighbours
# Mahalanobis distance
m_mahal = t(forwardsolve(t(chol(cov(m))), t(m)))
fdist(m_mahal, fmean(m_mahal))
sqrt(unattrib(mahalanobis(m, fmean(m), cov(m))))
# Distance of two vectors
x <- rnorm(1e6)
y <- rnorm(1e6)
microbenchmark::microbenchmark(
fdist(x, y),
fdist(x, y, nthreads = 2),
sqrt(sum((x-y)^2))
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.