r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.34k stars 297 forks source link

Add method for unnest? #564

Closed karldw closed 6 years ago

karldw commented 6 years ago

tidyr's unnest will soon allow for unnesting some list columns, but not others (https://github.com/tidyverse/tidyr/issues/328). That means you can unnest sf objects, as long as you tell unnest that the geometry column is a list column that should be preserved.

I can write a PR for this if you're okay adding another tidyverse method to sf.

Demo:

library(sf)
library(dplyr)
library(tidyr)
nc <- st_read(system.file("shape/nc.shp", package = "sf")) %>% 
  slice(1:3) %>%
  select(NAME) %>%
  mutate(y = strsplit(c("a", "d,e,f", "g,h"), ","))

# This will work soon, with no changes to sf:
unnest(nc, .preserve = geometry)

# The .preserve = geometry could be made automatic by adding an
# unnest method that knows to preserve the geometry column(s)
unnest(nc)
edzer commented 6 years ago

Yes, that would be welcome.