Learn R Programming

smerc (version 1.8.3)

knn: 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)

Value

An \(n \times k\) matrix of nearest neighbors.

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.

Examples

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

Run the code above in your browser using DataLab