Skip to contents

This function takes a set of polygons and calculates neighbors based on either polygon adjacency or polygon distance from centroid.

Usage

get_neighbors(polys, distance = "centroid", connect = TRUE)

Arguments

polys

A SpatialPolygons object containing the polygons for which to calculate neighbourhoods.

distance

The method by which to calculate distances between polygons. Set to "centroid" (default) to use the distance between centroids of polygons, or "polygon" to measure distances between the polygons themselves.

connect

Logical. Whether to create a neighborhood list based on polygon adjacency (TRUE) or polygon connectivity (FALSE).

Value

A 'neighbours' object created using the 'poly2nb' function from the spdep package. The neighbours object is a list of integer vectors, with one vector per spatial unit (polygon), describing indices into the polys

object to define neighbouring spatial units.

Examples

library(spdep)
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
#> Loading required package: sf
#> Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
data(nc.sids)
neighbors <- get_neighbors(polygons = nc.sids, distance="polygon", connect=FALSE)
#> Error in get_neighbors(polygons = nc.sids, distance = "polygon", connect = FALSE): unused argument (polygons = nc.sids)