knn: K nearest neighbors

View source: R/knn.R

knnR Documentation

K nearest neighbors

Description

knn returns the k nearest neighbors of the n coordinates in coords. The nearest neighbors are constructed to be self-inclusive, i.e., an observations is its closest neighbor.

Usage

knn(coords, longlat = FALSE, k = 1, d = NULL)

Arguments

coords

An n \times 2 matrix of centroid coordinates for the regions in the form (x, y) or (longitude, latitude) is using great circle distance.

longlat

The default is FALSE, which specifies that Euclidean distance should be used. If longlat is TRUE, then the great circle distance is used to calculate the intercentroid distance.

k

An integer indicating the maximum number of regions to inclue in a potential cluster. Default is 10

d

An n by n distance matrix. If provided, this is used instead of computing d based on coords and longlat.

Value

An n \times k matrix of nearest neighbors.

Examples

data(nydf)
coords <- nydf[, c("longitude", "latitude")]
knn(coords, longlat = TRUE, k = 4)

smerc documentation built on Oct. 13, 2022, 9:07 a.m.