[R-sig-Geo] How to calculate decadal average and percent change in raster stacks?

Loïc Dutrieux loic.dutrieux at wur.nl
Tue Dec 1 11:38:45 CET 2015


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:
> Dear all,
>
> I am working with yearly climate projections of rainfall that cover the period from 2010 to 2099:
>
> library(raster)
>
> # 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)
> s
>
> Given this raster stack, how can I calculate the average values per decade and percent change for each decade?
>
> I define decades here as 2010-2019, 2020-2029, 2030-2039 and so on until 2090-2099.
>
> I guess the real issue here is how to "split" a raster stack in decades. Any ideas?
> Thanks,
>   -- 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
>



More information about the R-sig-Geo mailing list