Skip to contents

An R package for approximate nearest neighbor search on dense and sparse data. Use it to build a neighbor graph, refine an existing graph, or build an index to query with new data.

The package adapts the core search methods from Leland McInnes’s PyNNDescent to R. It uses nearest neighbor descent and random partition trees. It also draws on FANNG and NGT.

Current Status

26 November 2026 Version 0.3.0 is on CRAN and adds a large number of performance improvements, utility functions for working with k-nearest neighbor graphs, correctness fixes for some metrics and other small improvements.

Installation

CRAN

install.packages("rnndescent")

Development version

# install.packages("pak")
pak::pak("jlmelville/rnndescent")

Building from source requires a C++17 compiler. See Rtools for Windows and the R build tools for macOS.

Example

library(rnndescent)

set.seed(42)
x <- iris[1:4]

# Find the 15 nearest neighbors of each observation
iris_knn <- rnnd_knn(x, k = 15)

# Build an index on reference rows and query it with the remaining rows
iris_index <- rnnd_build(x[1:100, ], k = 15)
iris_query_nn <- rnnd_query(iris_index, x[101:150, ], k = 15)

Missing Metrics

The metric argument takes the name of a built-in distance. This covers many PyNNDescent metrics, with gaps such as Mahalanobis and weighted Minkowski. See the metrics article for the full list.

Citation

Dong, W., Moses, C., & Li, K. (2011, March). Efficient k-nearest neighbor graph construction for generic similarity measures. In Proceedings of the 20th international conference on World wide web (pp. 577-586). ACM. doi.org/10.1145/1963405.1963487.

License

The R package is licensed under GPLv3 or later.

See Also