sf object won't convert to sp object with dplyr grouping variable

Created on 19 Jan 2018  路  2Comments  路  Source: r-spatial/sf

I am experiencing what I think is weird behaviour with group_by and as that may possibly be a bug. Load in data and packages:

library(sf)
library(dplyr)

nc = st_read(system.file("shape/nc.shp", package="sf"))

Now this is a trivial example but reproducible. If I try to group by NAME and FIPS then summarize and convert back to sp I get an error:

nc = st_read(system.file("shape/nc.shp", package="sf"))

nc_summarize <- nc %>%
  group_by(NAME, FIPS) %>%
  summarise()

nc_sp <- as(nc_summarize, "Spatial")

Error in st_sf(x, ..., agr = agr, sf_column_name = sf_column_name) :
no simple features geometry column present

Because dplyr drops that last grouping variable after summarize if I only include one, the conversion to sp happen no problem:

nc_summarize <- nc %>%
  group_by(NAME) %>%
  summarise()

nc_sp <- as(nc_summarize, "Spatial")

I don't think this is desired behaviour. This can be fixed with dplyr::ungroup() which objects may need to pass through before going through as.

Thanks for creating this wonderful package.

Session Info

> session_info()
Session info ------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.3 (2017-11-30)
 system   x86_64, mingw32             
 ui       RStudio (1.1.383)           
 language (EN)                        
 collate  English_Canada.1252         
 tz       America/Los_Angeles         
 date     2018-01-18                  

Packages ----------------------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                          
 assertthat   0.2.0      2017-04-11 CRAN (R 3.4.3)                  
 base       * 3.4.3      2017-12-06 local                           
 bindr        0.1        2016-11-13 CRAN (R 3.4.0)                  
 bindrcpp     0.2        2017-06-17 CRAN (R 3.4.0)                  
 class        7.3-14     2015-08-30 CRAN (R 3.4.3)                  
 classInt     0.1-24     2017-04-16 CRAN (R 3.4.2)                  
 compiler     3.4.3      2017-12-06 local                           
 datasets   * 3.4.3      2017-12-06 local                           
 DBI          0.7        2017-06-18 CRAN (R 3.4.0)                  
 devtools   * 1.13.4     2017-11-09 CRAN (R 3.4.3)                  
 digest       0.6.14     2018-01-14 CRAN (R 3.4.3)                  
 dplyr      * 0.7.4      2017-09-28 CRAN (R 3.4.3)                  
 e1071        1.6-8      2017-02-02 CRAN (R 3.4.1)                  
 glue         1.2.0      2017-10-29 CRAN (R 3.4.2)                  
 graphics   * 3.4.3      2017-12-06 local                           
 grDevices  * 3.4.3      2017-12-06 local                           
 grid         3.4.3      2017-12-06 local                           
 magrittr     1.5        2014-11-22 CRAN (R 3.4.3)                  
 memoise      1.1.0      2017-04-21 CRAN (R 3.4.0)                  
 methods    * 3.4.3      2017-12-06 local                           
 pillar       1.0.1      2017-11-27 CRAN (R 3.4.3)                  
 pkgconfig    2.0.1      2017-03-21 CRAN (R 3.4.0)                  
 R6           2.2.2      2017-06-17 CRAN (R 3.4.3)                  
 Rcpp         0.12.14    2017-11-23 CRAN (R 3.4.3)                  
 rlang        0.1.6      2017-12-21 CRAN (R 3.4.3)                  
 rstudioapi   0.7        2017-09-07 CRAN (R 3.4.3)                  
 sf         * 0.6-0      2018-01-06 CRAN (R 3.4.3)                  
 stats      * 3.4.3      2017-12-06 local                           
 swtext       0.0.1      2017-06-26 Github (boshek/swtext@4261578)  
 testthat   * 2.0.0      2017-12-13 CRAN (R 3.4.3)                  
 tibble       1.4.1      2017-12-25 CRAN (R 3.4.3)                  
 tools        3.4.3      2017-12-06 local                           
 udunits2     0.13       2016-11-17 CRAN (R 3.4.0)                  
 units        0.5-1      2018-01-08 CRAN (R 3.4.3)                  
 utils      * 3.4.3      2017-12-06 local                           
 withr        2.1.1.9000 2018-01-11 Github (jimhester/withr@df18523)
 yaml         2.1.16     2017-12-12 CRAN (R 3.4.3)           

Most helpful comment

Took me a bit, but it turned out that the culprit was a missing as.data.frame method for sf objects.

All 2 comments

Hi,

note that this also influences st_write, preventing saving the object to file:

library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.2, proj.4 4.9.3
suppressPackageStartupMessages(library(dplyr)
                               )
obj = st_read(system.file("shape/nc.shp", package="sf"), quiet = T)
obj_summarize <- obj %>%
    group_by(NAME, FIPS) %>%
    summarise()
sf::st_write(obj_summarize, tempfile(fileext = ".shp"))
#> Error in st_sf(x, ..., agr = agr, sf_column_name = sf_column_name): no simple features geometry column present

Simply adding this to st_write.sf could solve the problem:

# New line at read.R - line 256
if (inherits(obj, "grouped_df")) obj <- ungroup(obj)

Adding that we get:

# Result after the change
sf::st_write(obj_summarize, tempfile(fileext = ".shp"))
#> Writing layer `file3cdc36724928' to data source `/tmp/RtmpMwOXrj/file3cdc36724928.shp' using driver `ESRI Shapefile'
#> features:       100
#> fields:         2
#> geometry type:  Unknown (any)

I could do a PR on this if you wish.

Took me a bit, but it turned out that the culprit was a missing as.data.frame method for sf objects.

Was this page helpful?
0 / 5 - 0 ratings