There are several R packages, such as
RANN and
nabor that find the
The function nnt()
finds the
We use a simple example from the RANN:nn2()
documentation. We suppose
that both variables should be wrapped, on the ranges nnt()
uses the
function RANN::nn2()
(based on the L2 metric) to find the nearest
neighbours.
library(donut)
set.seed(20092019)
x1 <- runif(100, 0, 2 * pi)
x2 <- runif(100, 0, 3)
DATA <- data.frame(x1, x2)
ranges <- rbind(c(0, 2 * pi), c(0, 3))
query <- rbind(c(6, 1.3), c(2 * pi, 3), c(3, 1.5), c(4, 0))
library(RANN)
#> Warning: package 'RANN' was built under R version 4.2.3
res2 <- nnt(DATA, query, k = 8, torus = 1:2, ranges = ranges)
plot(res2)
To get the current released version from CRAN:
install.packages("donut")
See vignette("donut-vignette", package = "donut")
for an overview of
the package.