subs {raster} 函数不工作

subs {raster} function not working

我有一个像元值​​为 0、0.9 和 1 的栅格图层。我有一个数据框,第一列是像元 ID,第二列是值 0.936。(将来这些值在第二列中列可能不同,所以我确实需要将特定单元格替换为数据框中的相应值)

在 Gen_raster 中,我想用值 new_values[2] 替换与 ID new_values[1] 匹配的单元格的单元格值。

我尝试使用 subs() 函数(光栅包)执行此操作,但由于某些原因,值 new_values[1] 与 Gen_raster 的单元 ID 不匹配,因此没有任何结果碰巧单元格值为 Gen_raster。

我尝试使用 subs() 函数作为具有类似特征的示例(也使用栅格图层和具有两列 int 和 num 的数据框),这确实有效。这是我使用的代码以及栅格层和数据框的特征:

Gen_raster1<-subs(Gen_raster, new_values, subsWithNA=FALSE)    

其中 Gen_raster:

    class       : RasterLayer 
    dimensions  : 401, 292, 117092  (nrow, ncol, ncell)
    resolution  : 0.04166667, 0.04166667  (x, y)
    extent      : -79.04166, -66.87499, -4.249997, 12.45834  (xmin, xmax, ymin, ymax)
    coord. ref. : NA 
    data source : in memory
    names       : layer 
    values      : 0, 1  (min, max)

和new_values:

     'data.frame':  50 obs. of  2 variables:
     $ id: int  7444 7446 7735 7745 8331 8924 9185 9473 13261 13554 ...
     $ v : num  0.936 0.936 0.936 0.936 0.936 ...

这是我使用的示例,它确实有效:

    r <- raster(ncol=10, nrow=10)
    r[] <- round(runif(ncell(r)) * 10)
    df <- data.frame(id=2:8, v=c(10.003,10.03,11.006,11,12:14))
    x <- subs(r, df)
    x2 <- subs(r, df, subsWithNA=FALSE)

我看不出这两种方法有什么区别,因此我不明白为什么 new_values 和 Gen_raster 的小区 ID 不匹配..

有人有什么建议吗?会对我有很大帮助..谢谢!

subsGen_raster 中的单元格值 (0, 0.9, 1) 与 new_values 的第一列 (id) 匹配。看起来没有匹配项。

如果我理解你的话,你追求的是:

Gen_raster[new_values$id] <- new_values$v