Learn R Programming

spdep (version 0.1-10)

knearneigh: K nearest neighbours for spatial weights

Description

The function returns a matrix with the indices of regions belonging to the set of the k nearest neighbours of each other.

Usage

knearneigh(x, k=1)

Arguments

x
matrix of region point coordinates
k
number of nearest neighbours to be returned

Value

  • A list of class knn
  • nninteger matrix of region number ids
  • npnumber of input points
  • kinput required k
  • dimensionnumber of columns of x
  • xinput coordinates

Details

The underlying C code is based on the knn function in the class package in the VR bundle.

See Also

knn, dnearneigh, knn2nb

Examples

Run this code
data(columbus)
col.knn <- knearneigh(coords, k=4)
plotpolys(polys, bbs, border="grey")
plot(knn2nb(col.knn), coords, add=TRUE)
title(main="K nearest neighbours, k = 4")

Run the code above in your browser using DataLab