I have polygon shape data for German postcodes. For those postcode polygon I like to calculate various nearest neighbour measures. I have seen that procedures working with the sp package (using coordinates(), like knearneigh(coordinates(GER), k = 4)). I opt for sf spatial objects in R and are confused on how to implement neighbours here. Thank you
library(sf)
library(dplyr)
library(leaflet)
URL <- "https://downloads.suche-postleitzahl.org/v2/public/plz-5stellig.shp.zip"
# use GDAL virtual file systems to load zipped shapefile from remote url
GER_postcode <- paste0("/vsizip//vsicurl/", URL) %>% read_sf()
# country outline from giscoR
GER_outline <- giscoR::gisco_get_countries(country = "DE")
# subsample
GER_postcode_subsample <- GER_postcode %>% filter(substr(plz, 1, 1) %in% c(0, 1, 7))
# k nearest neighbours for sf dataframe
I found the answer in the
spdeppackage which contains the speaking functionpoly2nb(). Don't know why I havn't found this earlier.