R 如何对光栅NA值使用多种提取方法

R 如何对光栅NA值使用多种提取方法,r,raster,r-raster,R,Raster,R Raster,我对r和编码非常陌生,如果您能提供帮助,我将不胜感激 我有两个数据集: 一个光栅物体,加热周数(累积海面温度的遥感测量)。 一组坐标记录珊瑚漂白发生的位置 我已使用以下方法成功提取了每个坐标处的DHW值: graster::extract(mydata,coords,method=“simple”) 然而,有许多NA值。我认为这是因为许多坐标都靠近海岸,占据了主要的陆地填充像素 我想使用methods=“bilinear”为NA单元格插值,并使用methods=“simple”为非NA单元格插值

我对r和编码非常陌生,如果您能提供帮助,我将不胜感激

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

我已使用以下方法成功提取了每个坐标处的DHW值:
graster::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")

请使用
dput
功能发布数据集的第一行
i <- is.na(x)
x[i] <- y[i]