如何对栅格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,method="bilinear")
  } else {
    raster::extract(dhw_raster,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,method="bilinear")

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

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