Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tidyr::unnest method/support #340

Closed
jmlondon opened this issue May 14, 2017 · 3 comments
Closed

add tidyr::unnest method/support #340

jmlondon opened this issue May 14, 2017 · 3 comments

Comments

@jmlondon
Copy link

Thanks to 2f5914d, there is now a method for tidyr::nest(). That, obviously, brings up the question of how to support tidyr::unnest().

Here's an example of tidyr::nest() in action with sf objects

data(meuse, package = "sp")
meuse_sf <- st_as_sf(meuse,coords = c("x","y")) %>% group_by(landuse) %>% 
  nest()

> meuse_sf %>% head()
# A tibble: 6 × 2
  landuse               data
   <fctr>             <list>
1      Ah <tibble [39 × 12]>
2      Ga  <tibble [3 × 12]>
3      Ab  <tibble [8 × 12]>
4       W <tibble [50 × 12]>
5      Fh  <tibble [1 × 12]>
6      Ag  <tibble [5 × 12]>

> meuse_sf$data[[1]]
Simple feature collection with 39 features and 11 fields
geometry type:  POINT
dimension:      XY
bbox:           xmin: 178786 ymin: 329822 xmax: 181307 ymax: 333611
epsg (SRID):    NA
proj4string:    NA
# A tibble: 39 × 12
   cadmium copper  lead  zinc  elev       dist    om  ffreq
     <dbl>  <dbl> <dbl> <dbl> <dbl>      <dbl> <dbl> <fctr>
1     11.7     85   299  1022 7.909 0.00135803  13.6      1
2      8.6     81   277  1141 6.983 0.01222430  14.0      1
3      6.5     68   199   640 7.800 0.10302900  13.0      1
4      2.8     48   117   269 7.480 0.27709000   8.7      1
5      3.2     31   132   346 8.217 0.19009400   9.2      1
6      2.5     31   183   504 8.815 0.11393200   8.4      1
7      2.0     27   130   326 8.937 0.16833600   9.1      1
8      1.7     24    85   218 9.020 0.34232100   7.0      1
9      1.8     22    87   240 8.973 0.24986300   5.3      1
10     2.1     27    77   250 8.328 0.27162200   2.4      1
# ... with 29 more rows, and 4 more variables: soil <fctr>,
#   lime <fctr>, dist.m <dbl>, geometry <simple_feature>

In theory, we should be able to meuse_sf %>% tidyr::unnest() and get back our original ungrouped/unnested sf object. But,

> st_as_sf(meuse,coords = c("x","y"))  %>% class()
[1] "sf"         "data.frame"
> meuse_sf %>% unnest() %>% class()
[1] "tbl_df"     "tbl"        "data.frame"

In order to get back to an sf object, we need to

meuse_sf %>% unnest() %>%
  mutate(geometry = st_sfc(geometry)) %>% 
  st_as_sf()

Providing a generalized solution initially strikes me as non-trivial ... And, I have admittedly not spent much time thinking through how to implement such a solution. But, I'd like to, eventually, see a solution that mirrors the tidyr::nest/tidyr::unnest cycle.

@edzer
Copy link
Member

edzer commented May 14, 2017

I think that on the side of sf we can do little here:

  • meuse_sf is of class tbl_df, and I don't think that nest.sf should return a nested simple features object
  • when calling unnest on this, the nested sf objects become a tbl_df, and the geometry column becomes a list
  • essentially, tidyr drops all the attributes, hence we have to rebuild it using st_sfc
  • you lost crs and precision along the way in the example above

I believe the place to improve this would be in tidyr.

@jmlondon
Copy link
Author

That all makes sense to me. I'll think about this a bit more and maybe file an issue with tidyr.

Could you expand more on where/how precision was lost in the example? Is there a better approach that would preserve precision?

@edzer
Copy link
Member

edzer commented May 15, 2017

In the call to st_sfc you don't specify crs or precision; if these were set before, they're lost (for this particular case, only crs gets lost, precision is default, zero)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants