I'm trying to aggregate polygons at different geographic levels. As a hypothetical and reproducible example, let's suppose that I want to aggregate US county data set at the state level (of course, I can directly retrieve state-level polygons if this was my "actual" problem).
I've tried to do it by applying
st_union for each group, where the group is each state. The code seems to work fine, but when I try to use it for plotting or other data manipulation, R throws an error message as below.
Error in CPL_geos_is_empty(st_geometry(x)) : Not a matrix.
In general, if we have multiple levels of geography within a data (e.g., state, county, city, zip code, etc) and if we want to aggregate it at different levels, what's the best approach?
counties <- USAboundaries::us_counties() state_union <- counties %>% group_by(state_abbr) %>% summarise(geometry = sf::st_union(geometry)) %>% ungroup() %>% st_as_sf() plot(st_geometry(state_union))