r-如何使用光栅NA值的多种提取方法



我对r和编码很陌生,如果有任何帮助,我将不胜感激。

我有两个数据集:一个具有度加热周的光栅物体(一种累积海面温度的遥感测量(。一组坐标记录珊瑚白化发生的位置。

我使用以下方法成功提取了每个坐标处的DHW值:raster::extract(mydata, coords, method="simple")

然而,存在许多NA值。我认为这是因为许多坐标靠近海岸,并且主要占据陆地像素。

我想使用methods="bilinear"来对NA细胞的值进行插值,并使用methods="simple"来对非NA细胞进行插值。我希望输出是一个对象。

我写了以下函数:

if(is.na(dhw_raster[])){
raster::extract(dhw_raster, coords, method="bilinear")
} else {
raster::extract(dhw_raster, coords, method="simple")
}
}````


However, it returns only the method="simple" values, and this warning:
In if (is.na(dhw_raster[])) { :
the condition has length > 1 and only the first element will be used
Any advice would be great :)

您可能可以执行类似的操作

x <- raster::extract(mydata, coords, method="simple")
y <- raster::extract(mydata, coords, method="bilinear")

现在用y中的值替换x中缺失的值(然后使用x(

i <- is.na(x)
x[i] <- y[i]

最新更新