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

Frede Aakmann Tøgersen frtog at vestas.com
Tue Dec 1 10:23:24 CET 2015


Hi Thiago

There is no split method for RasterStacks but there is a subset method.

Is this useful?


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

decStart <- seq(2010, 2090, by = 10)

decades <- list()
for (strt in decStart)
    decades <- c(decades, list(seq(strt, (strt + 9), by = 1)))

ndx <- format(s at z$time, format = "%Y") %in% decades[[1]]

tmp <- subset(s, which(ndx))

tmp at z$time

Yours sincerely / Med venlig hilsen

Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 



-----Original Message-----
From: R-sig-Geo [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago V. dos Santos
Sent: 1. december 2015 09:14
To: R-sig-geo Mailing List
Subject: [R-sig-Geo] How to calculate decadal average and percent change in raster stacks?

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