简体   繁体   English

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

[英]How to use multiple extract methods for raster NA values

I'm quite new to r and coding and I'd appreciate any help please.我对 r 和编码很陌生,我很感激任何帮助。

I have two data sets: A raster object with degree heating weeks (a remotely sensed measure of cumulative sea surface temperature).我有两个数据集:一个带有度数加热周的栅格对象(累积海面温度的遥感测量)。 A set of coordinates recording where coral bleaching occurred.一组坐标记录发生珊瑚白化的地方。

I have successfully extracted the DHW values at each coordinate using the following: raster::extract(mydata, coords, method="simple")我已经使用以下raster::extract(mydata, coords, method="simple")成功提取了每个坐标处的 DHW 值: raster::extract(mydata, coords, method="simple")

However, there are many NA values.但是,有许多 NA 值。 I believe that this is because many coordinates are close to the coast and occupy predominantly land-filled pixels.我相信这是因为许多坐标靠近海岸并且主要占据填埋的像素。

I would like to use methods="bilinear" to interpolate values for the NA cells and methods="simple" for the non-NA cells.我想使用methods="bilinear"为 NA 单元插入值,为非 NA 单元插入methods="simple" I would like the output to be one object.我希望输出是一个对象。

I wrote the following function:我写了以下函数:

  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 :)

You can probably do something like this你可能可以做这样的事情

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

And now replace the missing values in x with the values in y (and then use x )现在用y的值替换x的缺失值(然后使用x

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM