Last active
May 4, 2022 11:48
-
-
Save jvieroe/a31ed23ccaafe7614c8beb8bb8d74fc7 to your computer and use it in GitHub Desktop.
find neighboring polygons with st_touches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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