setValues 栅格的用例
Use case of setValues raster
我最近偶然发现了 setValues()
形式的 raster
包,但我想知道这个函数的用例是什么。与普通子集和索引相比有什么优势吗:
r <- raster(ncol=10, nrow=10)
#setValues Function
r <- setValues(r, values=1, index=10)
#Normal indexing
r[10] <- 1
两种方式产生的结果相同。但是文档指出:
While you can access the 'values' slot of the objects directly, you
would do that at your own peril because when setting values, multiple
slots need to be changed; which is what these functions do.
作者这里的危险是什么意思?当我使用普通子集而不是 setValues 函数时,哪些插槽保持不变,或者在性能方面有什么优势?
setValues
的基本用例是,如果要将单元格值向量分配给(可能为空)RasterLayer
。例如
library(raster)
r <- raster(ncol=10, nrow=10)
r <- setValues(r, 1:100)
更符合 R 语言习惯的变体是
values(r) <- 1:100
索引通常用于少数单元格
r[1:5] <- NA
但您也可以使用它为所有单元格设置值
r[] <- 1:100
我最近偶然发现了 setValues()
形式的 raster
包,但我想知道这个函数的用例是什么。与普通子集和索引相比有什么优势吗:
r <- raster(ncol=10, nrow=10)
#setValues Function
r <- setValues(r, values=1, index=10)
#Normal indexing
r[10] <- 1
两种方式产生的结果相同。但是文档指出:
While you can access the 'values' slot of the objects directly, you would do that at your own peril because when setting values, multiple slots need to be changed; which is what these functions do.
作者这里的危险是什么意思?当我使用普通子集而不是 setValues 函数时,哪些插槽保持不变,或者在性能方面有什么优势?
setValues
的基本用例是,如果要将单元格值向量分配给(可能为空)RasterLayer
。例如
library(raster)
r <- raster(ncol=10, nrow=10)
r <- setValues(r, 1:100)
更符合 R 语言习惯的变体是
values(r) <- 1:100
索引通常用于少数单元格
r[1:5] <- NA
但您也可以使用它为所有单元格设置值
r[] <- 1:100