| sp_weights | R Documentation |
Constructs spatial weights (neighborhood structure) from sf geometries. Wraps spdep functions with a convenient interface.
sp_weights(
data,
type = c("queen", "rook", "knn", "distance"),
k = NULL,
d = NULL,
...
)
data |
An sf object with polygon or point geometries. |
type |
Type of weights. One of:
|
k |
Number of nearest neighbors. Required when |
d |
Distance threshold. Required when |
... |
Additional arguments passed to spdep functions. |
Choosing a weight type:
Use queen/rook for polygon data where physical adjacency matters
Use knn when you need guaranteed connectivity (no isolates) or for point data
Use distance for point data or when interaction depends on proximity
KNN weights always produce a connected graph (if k >= 1), making them useful for datasets with islands or disconnected polygons.
A neighbors list object (class "nb") compatible with spdep.
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Queen contiguity (default)
w_queen <- sp_weights(nc, type = "queen")
# K-nearest neighbors (guarantees connectivity)
w_knn <- sp_weights(nc, type = "knn", k = 6)
# Distance-based (e.g., 50km for projected data)
nc_proj <- st_transform(nc, 32119) # NC State Plane
w_dist <- sp_weights(nc_proj, type = "distance", d = 50000)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.