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

Thiago V. dos Santos thi_veloso at yahoo.com.br
Fri Jul 29 04:52:30 CEST 2016


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



More information about the R-sig-Geo mailing list