在R Raster中应用逻辑函数

时间:2017-03-19 13:35:03

标签: r r-raster

我在单层栅格中应用了逻辑函数:

f=function(x){if(x<0.4){return(1.88+209.5*x)} else {return(-239.6+810*x)}}
r=calc(x,f)
#where "x" is a single raster

执行后,返回:

Warning messages:
1: In if (x < 0.4) {:the condition has length > 1 and only the first element will be used
2: In if (x < 0.4) {:the condition has length > 1 and only the first element will be used

并仅执行前一个等式。这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

if并非设计用于获取逻辑条件的向量。另一方面,ifelse是:

f <- function(x) ifelse(x<0.4,1.88+209.5*x,-239.6+810*x)

例如:

> x <- 0.8*runif(5)
> x
[1] 0.7632327 0.2786816 0.2766365 0.6614716 0.7316004
> f(x)
[1] 378.61851  60.26380  59.83534 296.19197 352.99633

您必须对此进行测试,以了解它如何在栅格上运行。