Learn R Programming

sfdep (version 0.2.5)

st_knn: Calculate K-Nearest Neighbors

Description

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(geometry, k = 1, symmetric = FALSE, ...)

Value

a list of class nb

Arguments

geometry

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_dist_band()

Examples

Run this code
st_knn(sf::st_geometry(guerry), k = 8)

Run the code above in your browser using DataLab