I have rows of respondents within census polygons and am trying to generate first-order aggregated polygons, excluding the census unit a respondent resides in. Put another way, I want to combine neighboring polygons but exclude the polygon that touches all its neighbors.

st_union has been helpful in combining a polygon with its neighbors, but my trouble now is with dropping the central polygon.

I hope to remain within the sf environment.

  • 1
    Why are you including the "central polygon" in the union in the first place? Get the neighbour polygons and union that set, which won't include the "central polygon". – Spacedman Jan 7 at 23:02

You may use the DE9-IM relationship (see st_relate help page), and the function st_filter providing the relationship pattern, which in this case would be "FF2F11212":


df = read_sf("boundaries.gpkg")

# let's select a feature from the set, with []

df %>% st_filter(.,df[9,], .predicate = st_touches) %>%

As you may see, the feature is not included in the plot enter image description here

  • 1
    If you want to include polygons that touch at a single point, you can use st_touches for this instead of having to use st_relate with a DE9-IM string. Actually we don't know how the OP has computed adjacency... – Spacedman Jan 8 at 12:08
  • st_touches was returning the central polygon also, OP says they should be dropped; maybe he could use st_difference after the union, but that would more slower – Elio Diaz Jan 8 at 16:00
  • 1
    I can't replicate that: example(st_read); st_touches(nc$geom[5], nc$geom) doesn't show geometry 5 touching geometry 5, only 6, 9, 16 and 28. – Spacedman Jan 8 at 16:48
  • you are right, I think I inadvertently wrote st_intersects at first attempt, so I went for the DEM9-IM, I'll edit the answer, thanks – Elio Diaz Jan 8 at 17:32
  • @ElioDiaz This is very helpful; the st_filter predicate st_touches (or st_intersects) is absolutely what I need to obtain the neighbors list. My trouble now is with efficiently using st_union to combine the listed neighbors for each polygon ID. Any further assistance would be much appreciated. – peekochu Jan 8 at 21:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.