Skip to contents

Identifies the k nearest neighbors for given point geometry. If polygon geometry is provided, the centroids of the polygon will be used and a warning will be emitted.

Usage

st_knn(x, k = 1, symmetric = FALSE, ...)

Arguments

x

an sf or sfc object.

k

number of nearest neighbours to be returned

symmetric

default FALSE. Whether to force output of neighbours to be symmetric.

...

additional arguments to be passed to knearneigh().

Details

This function utilizes spdep::knearneigh() and spdep::knn2nb().

See also

Other neighbors: st_contiguity(), st_nb_band()

Examples

st_knn(sf::st_geometry(guerry), k = 8)
#> ! Polygon provided. Using centroid.
#> Neighbour list object:
#> Number of regions: 85 
#> Number of nonzero links: 680 
#> Percentage nonzero weights: 9.411765 
#> Average number of links: 8 
#> Non-symmetric neighbours list