Incorrect month order in zApply function
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