如何在r中找到具有不同连续变量范围的两个栅格的交集

How to find an intersection of two rasters with different ranges of continuous variables in r

我正在尝试使用 R 查找表示不同气候变量的栅格范围之间的交集。我能找到的最接近的答案是:

例如,如果我使用栅格包来分析 two bioclim variables(例如平均温度和降水量),我如何找到并绘制温度范围为 15-20 且降水量为500-750?

这是一个最小的、独立的、可重现的示例

library(raster)
temp <- prec <- raster()
values(temp) <- rep(1:30, each=ncell(temp)/30)
prec[,1:ncol(prec)] <- seq(0,nrow(prec)*10,10)

解决方案

tr <- reclassify(temp, c(-Inf, 15, NA, 15, 20, 1, 20, Inf, NA))
pr <- reclassify(prec, c(-Inf, 500, NA, 500, 750, 1, 750, Inf, NA))
zone <- overlay(tr, pr, fun=function(x,y) x*y)
# zone <- tr * pr # is equivalent

或使用 0 代替 NA

tr <- reclassify(temp, c(-Inf, 15, 0, 15, 20, 1, 20, Inf, 0))
pr <- reclassify(prec, c(-Inf, 500, 0, 500, 750, 1, 750, Inf, 0))
zoneTF <- tr & pr

现在你可能想做

s <- stack(prec, temp)
s <- mask(s, zone)