[R-sig-Geo] Incorrect month order in zApply function
chris english
englishchristophera at gmail.com
Sat Jul 30 00:35:46 CEST 2016
Thiago,
I understand you're in discussion with Robert about code changes; but,
absent these, why wouldn't Ben's solution be sufficient, assuming the code
change might simply issue a warning that the names output will be
alphanumeric and a second step might be required to conform for
presentation or analytical purposes. Just wondering. Thanks.
Chris
On Jul 29, 2016 22:20, "Thiago V. dos Santos via R-sig-Geo" <
r-sig-geo at r-project.org> wrote:
Thanks everyone for their suggestions.
Reordering the output of zApply would be a WRONG solution, because the
layers are already ordered from Jan to Dec.
It is just the NAMES that are misaligned, which can cause a bit of
confusion.
I already contacted Robert about this issue and we should see a solution
soon.
Greetings,
-- Thiago V. dos Santos
PhD student
Land and Atmospheric Science
University of Minnesota
On Friday, July 29, 2016 2:09 PM, Kay Kilpatrick <
kkilpatrick at rsmas.miami.edu> wrote:
Hi
The various Apply functions always return the output in simple
alphabetical order when the factor label is a character, regardless of
the order of the factor in the calling argument.
Ben had a correct solution --- simply reorder the output from Zapply.
Alternatively you could use a numeric representation for the month label
using by = as.numeric(months(x). The output would then be in increasing
numeric order from 1 -12.
I do not believe Vijay's suggestion is proper. It does not reorder the
output --- instead it changes the label of a given monthly mean eg. The
value for Jan. would then be labeled as Apr.
k
On 7/28/16 10:52 PM, Thiago V. dos Santos via R-sig-Geo 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://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mailman_listinfo_r-2Dsig-2Dgeo&d=CwICAg&c=y2w-uYmhgFWijp_IQN0DhA&r=VXzrzLaooo912e5bVVEO_z5cHQXu_22lipAXBkfCXjM&m=tzdb3kN3Nxjo7NbPkPVqtF269bJen1bf3AzSHEwd7CA&s=zuokuoKIDT6cOaBD2J6ePy1klmf1J3-f3BynP_G5I-g&e=
--
Katherine (Kay) Kilpatrick
University of Miami/RSMAS
Ocean Science Department
Satellite Remote Sensing Laboratory
MSC 231
4600 Rickenbacker Cswy
Miami, Fl
ph: 305-962-3069
kkilpatrick at rsmas.miami.edu
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
_______________________________________________
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