r-spatial / stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
https://r-spatial.github.io/stars/
Apache License 2.0
562 stars 94 forks source link

Output of stars object after aggregate doesn't readily turn into a tibble #270

Closed kendonB closed 4 years ago

kendonB commented 4 years ago

Converting a stars object to a tibble after using aggregate does not seem to work. See below:

library(tidyverse)
library(stars)
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.6.2, GDAL 2.2.3, PROJ 4.9.3
library(sf)
prec_file = system.file("nc/test_stageiv_xyt.nc", package = "stars")
prec = read_ncdf(prec_file, curvilinear = c("lon", "lat"), ignore_bounds = TRUE)
#> no 'var' specified, using Total_precipitation_surface_1_Hour_Accumulation
#> other available variables:
#>  time_bounds, lon, lat, time
#> No projection information found in nc file. 
#>  Coordinate variable units found to be degrees, 
#>  assuming WGS84 Lat/Lon.
read_sf(system.file("gpkg/nc.gpkg", package = "sf"), "nc.gpkg") %>%
  st_transform(st_crs(prec)) -> nc # transform from NAD27 to WGS84
# no 'var' specified, using Total_precipitation_surface_1_Hour_Accumulation
a = aggregate(prec, by = nc, FUN = max)
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
class(a)
#> [1] "stars"
a %>% 
  as_tibble()
#> Warning: Outer names are only allowed for unnamed scalar atomic inputs
#> Error: Argument 1 must be a data frame or a named atomic vector, not a XY/MULTIPOLYGON/sfg

a %>% 
  as.data.frame() %>% 
  as_tibble()
#> # A tibble: 2,300 x 3
#>                                   geom time                Total_precipitation_…
#>                     <MULTIPOLYGON [°]> <dttm>                              <dbl>
#>  1 (((-81.47258 36.23448, -81.54067 3… 2018-09-13 18:30:00                 NA   
#>  2 (((-81.2397 36.36549, -81.24051 36… 2018-09-13 18:30:00                 NA   
#>  3 (((-80.45612 36.2427, -80.47617 36… 2018-09-13 18:30:00                 NA   
#>  4 (((-76.00862 36.31975, -76.017 36.… 2018-09-13 18:30:00                  0.5 
#>  5 (((-77.21735 36.24114, -77.2343 36… 2018-09-13 18:30:00                 10.1 
#>  6 (((-76.74474 36.23407, -76.98037 3… 2018-09-13 18:30:00                  4.25
#>  7 (((-76.00862 36.31975, -75.95683 3… 2018-09-13 18:30:00                  1.88
#>  8 (((-76.56218 36.34072, -76.60391 3… 2018-09-13 18:30:00                  3.88
#>  9 (((-78.30848 36.26019, -78.28265 3… 2018-09-13 18:30:00                  1.75
#> 10 (((-80.02544 36.25037, -80.45279 3… 2018-09-13 18:30:00                 NA   
#> # … with 2,290 more rows

Created on 2020-03-31 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 3.6.3 (2020-02-29) #> os Ubuntu 18.04.4 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_NZ.UTF-8 #> ctype en_NZ.UTF-8 #> tz Pacific/Auckland #> date 2020-03-31 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> abind * 1.4-5 2016-07-21 [1] CRAN (R 3.6.0) #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.0) #> backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.1) #> broom 0.5.4 2020-01-27 [1] CRAN (R 3.6.2) #> callr 3.4.2 2020-02-12 [1] CRAN (R 3.6.2) #> cellranger 1.1.0 2016-07-27 [1] CRAN (R 3.6.0) #> class 7.3-15 2019-01-01 [4] CRAN (R 3.5.2) #> classInt 0.4-2 2019-10-17 [1] CRAN (R 3.6.2) #> cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3) #> colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> DBI 1.1.0 2019-12-15 [1] CRAN (R 3.6.2) #> dbplyr 1.4.2 2019-06-17 [1] CRAN (R 3.6.0) #> desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.1) #> devtools 2.2.2 2020-02-17 [1] CRAN (R 3.6.2) #> digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.2) #> dplyr * 0.8.4 2020-01-31 [1] CRAN (R 3.6.2) #> e1071 1.7-3 2019-11-26 [1] CRAN (R 3.6.2) #> ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.1) #> evaluate 0.14 2019-05-28 [1] CRAN (R 3.6.0) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.2) #> forcats * 0.4.0 2019-02-17 [1] CRAN (R 3.6.0) #> fs 1.3.1 2019-05-06 [1] CRAN (R 3.6.0) #> generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.0) #> ggplot2 * 3.2.1 2019-08-10 [1] CRAN (R 3.6.1) #> glue 1.3.2 2020-03-12 [1] CRAN (R 3.6.3) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.0) #> haven 2.2.0 2019-11-08 [1] CRAN (R 3.6.1) #> highr 0.8 2019-03-20 [1] CRAN (R 3.6.0) #> hms 0.5.3 2020-01-08 [1] CRAN (R 3.6.2) #> htmltools 0.4.0 2019-10-04 [1] CRAN (R 3.6.2) #> httr 1.4.1 2019-08-05 [1] CRAN (R 3.6.1) #> jsonlite 1.6.1 2020-02-02 [1] CRAN (R 3.6.2) #> KernSmooth 2.23-16 2019-10-15 [4] CRAN (R 3.6.1) #> knitr 1.28 2020-02-06 [1] CRAN (R 3.6.2) #> lattice 0.20-40 2020-02-19 [1] CRAN (R 3.6.2) #> lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.0) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3) #> lubridate 1.7.4 2018-04-11 [1] CRAN (R 3.6.0) #> lwgeom 0.2-1 2020-01-31 [1] CRAN (R 3.6.2) #> magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.0) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.1) #> modelr 0.1.6 2020-02-22 [1] CRAN (R 3.6.2) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.0) #> ncmeta 0.2.0 2019-10-22 [1] CRAN (R 3.6.3) #> nlme 3.1-144 2020-02-06 [4] CRAN (R 3.6.2) #> pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.2) #> pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 3.6.1) #> pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.1) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.2) #> processx 3.4.2 2020-02-09 [1] CRAN (R 3.6.2) #> ps 1.3.2 2020-02-13 [1] CRAN (R 3.6.2) #> purrr * 0.3.3 2019-10-18 [1] CRAN (R 3.6.1) #> R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.1) #> Rcpp 1.0.4 2020-03-17 [1] CRAN (R 3.6.3) #> readr * 1.3.1 2018-12-21 [1] CRAN (R 3.6.0) #> readxl 1.3.1 2019-03-13 [1] CRAN (R 3.6.0) #> remotes 2.1.1 2020-02-15 [1] CRAN (R 3.6.2) #> reprex 0.3.0 2019-05-16 [1] CRAN (R 3.6.0) #> rlang 0.4.5 2020-03-01 [1] CRAN (R 3.6.3) #> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.2) #> RNetCDF 2.1-1 2019-10-20 [1] CRAN (R 3.6.3) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1) #> rvest 0.3.5 2019-11-08 [1] CRAN (R 3.6.2) #> scales 1.1.0 2019-11-18 [1] CRAN (R 3.6.2) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1) #> sf * 0.9-1 2020-03-31 [1] Github (r-spatial/sf@8cc2adf) #> stars * 0.4-1 2020-03-31 [1] Github (r-spatial/stars@e26ceb8) #> stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2) #> stringr * 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> testthat 2.3.1 2019-12-01 [1] CRAN (R 3.6.2) #> tibble * 3.0.0 2020-03-31 [1] Github (tidyverse/tibble@c6a3f71) #> tidyr * 1.0.2 2020-01-24 [1] CRAN (R 3.6.2) #> tidyselect 1.0.0 2020-01-27 [1] CRAN (R 3.6.2) #> tidyverse * 1.3.0 2019-11-21 [1] CRAN (R 3.6.2) #> units 0.6-6 2020-03-16 [1] CRAN (R 3.6.3) #> usethis 1.5.1 2019-07-04 [1] CRAN (R 3.6.1) #> utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.0) #> vctrs 0.2.4 2020-03-10 [1] CRAN (R 3.6.3) #> withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.0) #> xfun 0.12 2020-01-13 [1] CRAN (R 3.6.2) #> xml2 1.2.2 2019-08-09 [1] CRAN (R 3.6.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.2) #> #> [1] /home/kendonb/R/x86_64-pc-linux-gnu-library/3.6 #> [2] /usr/local/lib/R/site-library #> [3] /usr/lib/R/site-library #> [4] /usr/lib/R/library ```
edzer commented 4 years ago

Should work now - thanks for the reprex!