I need to extract the neighboring polygons for each polygon in an sf dataset.
Here's a quick example:
library(tidyverse)
library(sf)
demo(nc, ask = FALSE, verbose = FALSE)
nc <- nc %>%
mutate(polygon_id = row_number())
I have managed to extract the neighbors with sf::st_intersects
neighbors <- st_intersects(nc, nc)
neighbors[[5]]
[1] 5 6 9 16 28
The issue is that each polygon (here, 5) is being included in the list of neighbors. Using only one nc dataset gives me the same result
neighbors <- st_intersects(nc)
neighbors[[5]]
[1] 5 6 9 16 28
Any tips on how to filter out the actual polygon from the list of adjacent/neighboring polygons?