0

I have a shapefile (.shp) where each geographic unit is (the equivalent of) a county. I loaded it and called it gdfc. I would like to combine counties to create an sf object where each observation is a state. I have tried the following code:

library(dplyr)
library(sf)

goems <- map(sort(unique(gdfc[['id_state']])), 
             function(s) st_union(filter(gdfc, id_state == s))) # Dissolving boundaries inside states
gdfs <- data.frame(id_state = sort(unique(gdfc[['id_state']]))) # Data frame with state identifiers
st_geometry(gdfs) <- st_sfc(geoms)                              # Assigning (i.e., attempting to) dissolved geometries

However, it did not work, as I got the following error:

Error in vapply(lst, class, rep(NA_character_, 3)) : values must be length 3, but FUN(X[[1]]) result is length 1

How should I do to create the set of larger geographic units (states) from the smaller ones (counties)?

DOS
  • 29
  • 7
  • 4
    Consider `dplyr::summarize()` on a {sf} data frame. You will find it explained in more detail here https://www.jla-data.net/eng/merging-geometry-of-sf-objects-in-r/ – Jindra Lacko Jan 29 '22 at 09:17
  • Thank you very much for the explanation; the explanation provided in the link is very good. It worked smoothly. – DOS Jan 29 '22 at 13:44
  • and I thank you for your kind words! – Jindra Lacko Jan 29 '22 at 13:47

0 Answers0