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)?