我需要通过向其添加新的计算列来操作下面的栅格。 我将栅格转换为 data.frame 并添加了列。 问题:这是我如何通过转换为 data.frame 或 我可以将列直接添加到栅格并保存吗? 考虑下面的虚拟示例:
library(terra)
#> terra 1.7.23
# Mock data
f <- system.file("extdata/asia.tif", package = "tidyterra")
xx <- rast(f)
xx
rast_df <- as.data.frame(xx,na.rm=TRUE, cells = TRUE)
head(rast_df)
rast_df$col1 <- with(rast_df,cell*5)
rast_df$col2 <- with(rast_df,cell*20)
rast_df$col3 <- with(rast_df,cell*50)
How can I save these new added columns to the original xx rast?
I will be adding at least 20 columns to the xx rast.
如果要向栅格添加新列,则必须更改栅格的维度:
dim(xx)
[1] 232 432 1
dim(xx) <- c(232,433,1) # one column added
然后您可以通过
对该新列执行计算xx[,433] <- xx[,1]*5 # silly example
但请注意,这会改变光栅的分辨率。