Skip to content
Prev 23741 / 29559 Next

How to calculate decadal average and percent change in raster stacks?

Hi Thiago,

This should work for aggregating to decades.

library(raster)
library(lubridate)

# Create the date sequence
idx <- seq(as.Date("2010/1/1"), as.Date("2099/12/31"), by = "year")

# Create raster stack and apply the date
r <- raster(ncol=360, nrow=180)
s <- stack(lapply(1:length(idx), function(x) setValues(r, runif(ncell(r)))))
s <- setZ(s, idx)

# Aggregate to decades
zApply(s, by = function(x) year(x) %/% 10 * 10, fun = mean)


By calculate percent change for each decade, do you mean between decades 
or within?

Cheers,
Lo?c
On 12/01/2015 09:14 AM, Thiago V. dos Santos wrote: