[R-sig-Geo] Incorrect month order in zApply function

Ben Tupper btupper at bigelow.org
Fri Jul 29 17:12:38 CEST 2016


Hi,

Might you simply reorder the stack after zApply()?

> x2 <- x[[month.name]]
> names(x2)
 [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" 

Cheers,
Ben



> On Jul 29, 2016, at 10:52 AM, Dominik Schneider <dosc3612 at colorado.edu> wrote:
> 
> Sorry I'm not sure how to fix this, but it looks like the months are  in
> alphanumeric order, indicating they are being treated as a factor. Might be
> worth a bug report?
> 
> 
> 
> On Thursday, July 28, 2016, Thiago V. dos Santos via R-sig-Geo <
> r-sig-geo at r-project.org> wrote:
> 
>> Dear all,
>> 
>> I am using the raster package to calculate monthly averages of climatic
>> variables.
>> 
>> Essentially, this is the function I use:
>> 
>> library(raster)
>> 
>> # Create date sequence
>> idx <- seq(as.Date("1996/1/1"), as.Date("2010/12/31"), by = "day")
>> 
>> # Create raster stack and assign dates
>> r <- raster(ncol=5, nrow=5)
>> s <- stack(lapply(1:length(idx), function(x) setValues(r,
>> runif(ncell(r)))))
>> s <- setZ(s, idx)
>> 
>> # Calculate monthly average
>> x <- zApply(s, by=months, mean, name=month.abb[])
>> 
>> names(x)
>> [1] "April" "August" "December" "February" "January" "July" "June"
>> [8] "March" "May" "November" "October" "September"
>> getZ(x)
>> [1] "April" "August" "December" "February" "January" "July" "June"
>> [8] "March" "May" "November" "October" "September"
>> 
>> 
>> The problem here is the output of both names(x) and getZ(x). It looks like
>> a random month order is returned (even though I provide the labels), which
>> makes me confused about the results.
>> 
>> 
>> By performing the same calculation in a different software and comparing
>> the results, I came to realize that the order of months for the results by
>> raster should, in fact, be Jan-Feb-Mar-Apr-May-Jun-Jul-Aug-Sep-Oct-Nov-Dec
>> 
>> How can I control the way raster delivers the object names after using
>> zApply, in order to sort the months in the "natural" order?
>> 
>> Greetings, -- Thiago V. dos Santos
>> 
>> PhD student
>> Land and Atmospheric Science
>> University of Minnesota
>> 
>> _______________________________________________
>> R-sig-Geo mailing list
>> R-sig-Geo at r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> 
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org

Report Gulf of Maine jellyfish sightings to jellyfish at bigelow.org or tweet them to #MaineJellies -- include date, time, and location, as well as any descriptive information such as size or type.  Learn more at https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/



More information about the R-sig-Geo mailing list