[R-sig-Geo] How to perform cross-year date operations on rasters?
Michael Sumner
mdsumner at gmail.com
Thu Nov 5 07:14:19 CET 2015
On Thu, 5 Nov 2015 at 09:38 Thiago V. dos Santos <thi_veloso at yahoo.com.br>
wrote:
> Dear all,
>
> Consider that I have a raster stack with daily values. How can I perform
> date operations covering a time interval that crosses years?
>
> For example, I want to sum the values from every october-to-january period
> in this sample raster:
>
> library(raster)
>
> # Create a rasterStack similar to cmip5 - same dimensions and layer names
> r <- raster(ncol=180, nrow=90)
> s <- stack(lapply(1:1825, function(x) setValues(r, runif(ncell(r)))))
>
> # Apply time stamps to raster
> #x <- as.Date(c("2010-01-01","2014-12-31"),format="%Y-%m-%d")
> #difftime(x[2], x[1], units="days")
> idx <- seq(as.Date("2010/1/1"), by = "day", length.out = 1825)
> s <- setZ(s, idx)
> s
>
>
>
You can subset on the dates, by running a test on the date values:
ldates <- format(getZ(s), "%m") %in% c("10", "11", "01")
subsetting the object
subset(s, which(ldates))
and finally calculating what you want
calc(subset(s, which(ldates)), sum)
HTH
> Thanks in advance,
> -- 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
>
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list