Description Usage Arguments Value Examples
k nearest neighbor search with custom distance function.
1 2 
data 
Data matrix 
k 
Number of nearest neighbors 
... 
Unused. All parameters to the right of the 
query 
Query matrix. In 
distance 
Distance metric to use. Allowed measures: Euclidean distance (default), cosine distance (1corr(c_1, c_2)) or rank correlation distance (1corr(rank(c_1), rank(c_2))) 
sym 
Return a symmetric matrix (as long as query is NULL)? 
A list
with the entries:
index
A nrow(data) \times k integer matrix containing the indices of the k nearest neighbors for each cell.
dist
A nrow(data) \times k double matrix containing the distances to the k nearest neighbors for each cell.
dist_mat
A dgCMatrix
if sym == TRUE
,
else a dsCMatrix
(nrow(query) \times nrow(data)).
Any zero in the matrix (except for the diagonal) indicates that the cells in the corresponding pair are close neighbors.
1 2 3 4 5 6 7 8 9 10 11 12 13  # The default: symmetricised pairwise distances between all rows
pairwise < find_knn(mtcars, 5L)
image(as.matrix(pairwise$dist_mat))
# Nearest neighbors of a subset within all
mercedeses < grepl('Merc', rownames(mtcars))
merc_vs_all < find_knn(mtcars, 5L, query = mtcars[mercedeses, ])
# Replace row index matrix with row name matrix
matrix(
rownames(mtcars)[merc_vs_all$index],
nrow(merc_vs_all$index),
dimnames = list(rownames(merc_vs_all$index), NULL)
)[, 1] # 1st nearest neighbor is always the same row

Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.