Skip to content

Instantly share code, notes, and snippets.

@jvieroe
Last active May 4, 2022 11:48
Show Gist options
  • Save jvieroe/a31ed23ccaafe7614c8beb8bb8d74fc7 to your computer and use it in GitHub Desktop.
Save jvieroe/a31ed23ccaafe7614c8beb8bb8d74fc7 to your computer and use it in GitHub Desktop.
find neighboring polygons with st_touches
library(tidyverse)
library(sf)
demo(nc, ask = FALSE, verbose = FALSE)
nc <- nc %>%
mutate(polygon_id = row_number())
ggplot() +
geom_sf(data = nc)
# with st_intersects
neighbors <- st_intersects(nc)
neighbors[[5]]
# with st_touches
neighbors <- st_touches(nc)
neighbors[[5]]
# add list to your data
nc <- nc %>%
mutate(p_id = row_number()) %>%
select(NAME, p_id)
nc <- nc %>%
mutate(touches = st_touches(.))
nc %>%
filter(p_id == 5) %>%
pull(touches)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment