2

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
  • Have you already seen this article Error in aggregate.data.frame : arguments must have same length ? – Taras Feb 20 at 13:34
  • 1
    Where does st_dimensions and st_as_stars come from? The latest CRAN versions of sf, spacetime, and rgeos do not have them. – Spacedman Feb 20 at 16:00
  • @ Taras No, thanks I had not seen that, but it also does not help me further. @Spacedman Sorry for that. They are both from the stars package. Forgot to include that. I installed all the packages in the last week so they are most likely up to date. Versions: sf: 0.7-2, rgeos 0.4-2, spacetime 1.2-2 and stars 0.2-0. – JonJup Feb 21 at 7:56
  • Perhaps aggregate method for stars objects will become available in the next version, stars 0.3-0. – Kazuhito Feb 22 at 22:03
  • Hi JonJup stars 0.3-0 is now on CRAN. – Kazuhito Feb 25 at 0:11
1

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   
  • Yes, now it works. Thanks a lot. Looks like the content of the book was ahead of the released package version. – JonJup Feb 25 at 15:35
  • @JonJup It will be nice if you create a new tag stars for us. I imagine it will soon become a popular tag. – Kazuhito Feb 26 at 10:22
  • Unfortunately, I cannot create new tags yet. My reputation is too low. – JonJup Feb 26 at 11:33
  • Thanks @JonJup and sorry I did not know that. Let me give it a try. – Kazuhito Feb 26 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.