# nneighb: Find nearest neighbors In kdtools: Tools for Working with Multidimensional Data

## Description

Find nearest neighbors

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29``` ```kd_nearest_neighbors(x, v, n, ...) ## S3 method for class 'matrix' kd_nearest_neighbors(x, v, n, cols = NULL, alpha = 0, ...) ## S3 method for class 'arrayvec' kd_nearest_neighbors(x, v, n, ...) ## S3 method for class 'data.frame' kd_nearest_neighbors(x, v, n, cols = NULL, w = NULL, ...) kd_nn_indices(x, v, n, ...) ## S3 method for class 'arrayvec' kd_nn_indices(x, v, n, distances = FALSE, ...) ## S3 method for class 'matrix' kd_nn_indices(x, v, n, cols = NULL, distances = FALSE, alpha = 0, ...) ## S3 method for class 'data.frame' kd_nn_indices(x, v, n, cols = NULL, w = NULL, distances = FALSE, ...) kd_nearest_neighbor(x, v) ## S3 method for class 'matrix' kd_nearest_neighbor(x, v) ## S3 method for class 'arrayvec' kd_nearest_neighbor(x, v) ```

## Arguments

 `x` an object sorted by `kd_sort` `v` a vector specifying where to look `n` the number of neighbors to return `...` ignored `cols` integer or character vector or formula indicating columns `alpha` approximate neighbors within (1 + alpha) `w` distance weights `distances` return distances as attribute if true

## Value

 `kd_nearest_neighbors` one or more rows from the sorted input `kd_nn_indices` a vector of row indices indicating the result `kd_nearest_neighbor` the row index of the neighbor

## Examples

 ```1 2 3 4 5 6 7 8``` ```if (has_cxx17()) { x = matrix(runif(200), 100) y = matrix_to_tuples(x) kd_sort(y, inplace = TRUE) y[kd_nearest_neighbor(y, c(1/2, 1/2)),] kd_nearest_neighbors(y, c(1/2, 1/2), 3) y[kd_nn_indices(y, c(1/2, 1/2), 5),] } ```

kdtools documentation built on Oct. 8, 2021, 9:07 a.m.