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

Vijay Lulla vijaylulla at gmail.com
Fri Jul 29 19:35:22 CEST 2016


Maybe like this?

R> m <- function(x) ordered(months(x),labels=month.name)
R> x1 <- zApply(s, by=m, mean, name=month.abb[])
R> names(x1) # Appears to have natural ordering
R> all(x[] == x1[]) # TRUE

I'm not sure how using factors will impact other aspects of your project.
HTH,
Vijay.

On Thu, Jul 28, 2016 at 10:52 PM, 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]]



More information about the R-sig-Geo mailing list