[R-sig-Geo] comparing one raster to a stack and condition

karsten karsten at terragis.net
Wed Dec 13 22:13:14 CET 2017


Hi All,
 
I am trying to compare one precipitation raster to a stack of precipitation
raster and would like to create a result raster with a count of how often
the raster value is below those of the stack.
So far I have the following code:
 
----------------------------------------------
 
library(zoo)
library(raster)
# create raster stack for Januaries
alltiffs = list.files(getwd(), pattern="*\\.tif$", full.names=TRUE) 

#filter the ones with 01 in it for january
januarygrids =  alltiffs[grepl("*.01.*", alltiffs)]
 
# Create raster stack of grids
r <- stack(januarygrids, quick=TRUE)
 
# set current january layer to compare with
currentmonth <- "es_af.2017.01.tif"
currentmonthraster <- raster(currentmonth)
 
# function to count how oftetn current ratser is below values in stack,
input r and currentmonthraster 
belowCurrentRaster <- function(x, y) {
  sum(x > y)
}
 
-------------------------------------------------------
 
Now I thought I could use zApply or overlay to get the count from the
belowCurrentRaster function and write those counts into a result raster.
But I could not figure out how to make this work.
Any ideas appreciated.
 
Cheers 
Karsten Vennemann
Terra GIS LTD


	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list