Skip to content
Prev 24724 / 29559 Next

Incorrect month order in zApply function

Hi,

I reworked your example a bit so that it encompasses just one year, and the value of each layer is the month number for the Z index.  Sure enough, the mean month number values look like they are mismatched relative to the layer name.  Example below - sorry about line-wrapping.  Uhoh.

Cheers,
Ben

library(raster)

# Create date sequence
idx <- seq(as.Date("1996/1/1"), as.Date("1996/12/31"), by = "day")

mon_num <- as.numeric(format(idx, "%m"))

# Create raster stack and assign dates
r <- raster(ncol=5, nrow=5)
s <- stack(
    lapply(1:length(idx), 
        function(x) setValues(r, rep(mon_num[x],ncell(r)))
        )
    )
s <- setZ(s, idx)

# Calculate monthly average
x <- zApply(s, by=months, mean, name=month.abb[])

x
# class       : RasterBrick 
# dimensions  : 5, 5, 25, 12  (nrow, ncol, ncell, nlayers)
# resolution  : 72, 36  (x, y)
# extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
# data source : in memory
# names       : April, August, December, February, January, July, June, March, May, November, October, September 
# min values  :     1,      2,        3,        4,       5,    6,    7,     8,   9,       10,      11,        12 
# max values  :     1,      2,        3,        4,       5,    6,    7,     8,   9,       10,      11,        12
R version 3.3.1 (2016-06-21)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] raster_2.5-8 sp_1.2-3    

loaded via a namespace (and not attached):
[1] rgdal_1.1-10    Rcpp_0.12.6     grid_3.3.1      lattice_0.20-33
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/