R Aggregate Function Error – How to Fix Error from Aggregate Function in R Package SF

rsfstars

I am currently reading the new (and free!) book by Edzer Pebesma and Roger Bivand (you can find it here).

In chapter four they describe how one can aggregate all the measurement stations within one state. I follow there code but get an error. How so?

library(sf)
library(spacetime)
library(rgeos)
library(stars)
# dataset from spacetime package
data(air)
d = st_dimensions(station = st_as_sfc(stations), time = dates)
aq = st_as_stars(list(PM10 = air), dimensions = d)
a = aggregate(aq, st_as_sf(DE_NUTS1), mean, na.rm = TRUE)

The last line returns the error:

Error in aggregate.data.frame(as.data.frame(x), ...) : 
Arguments must have same length

Best Answer

You can check if aggregate method is supported by methods().

# stars 0.2-0
> library(stars)
> methods(class= "stars")
 [1] [              [<-            adrop          aperm          as.data.frame  c              coerce        
 [8] cut            dim            dimnames       dimnames<-     filter         image          initialize    
[15] is.na          Math           merge          Ops            plot           print          show          
[22] slotsFromS3    split          st_apply       st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[29] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_transform   st_write  

Ta-da! stars 0.3-0 is on CRAN today (25/FEB/2019).

# stars 0.3-0
> library(stars)
> methods(class= "stars")
 [1] $<-            [              [<-            adrop          aggregate      aperm         
 [7] as.data.frame  c              coerce         cut            dim            dimnames      
[13] dimnames<-     image          initialize     is.na          Math           merge         
[19] Ops            plot           print          show           slotsFromS3    split         
[25] st_apply       st_area        st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[31] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_geometry   
[37] st_redimension st_transform   write_stars 

You can see aggreagte is listed. Yay!


By the way, if you add tidyverse, the list expands like:

> library(tidyverse)
> library(stars)
> methods(class= "stars")
 [1] $<-            [              [<-            adrop          aggregate      aperm         
 [7] as.data.frame  as.tbl_cube    c              coerce         cut            dim           
[13] dimnames       dimnames<-     filter         image          initialize     is.na         
[19] Math           merge          mutate         Ops            plot           print         
[25] pull           select         show           slice          slotsFromS3    split         
[31] st_apply       st_area        st_as_sf       st_as_sfc      st_as_stars    st_bbox       
[37] st_coordinates st_crop        st_crs         st_crs<-       st_dimensions  st_geometry   
[43] st_redimension st_transform   write_stars