我正在尝试使用gstat()
and创建反距离加权栅格raster::interpolate()
。我在将列名传递给 gstat 函数的公式参数时遇到问题。
对列名进行硬编码就可以了:
gs <- gstat(formula=v1~1, locations = data)
r <- raster(shape, res=1000, crs = crs(data))
idw <- raster::interpolate(r, gs)
idwr <- mask(r, gs)
plot(idwr)
将其包装到一个函数中,以便我可以遍历多个列名会引发错误:
apply_gstat <- function(col_name, data = data, shape = shape) {
gs <- gstat(formula=col_name~1, locations = data)
r <- raster(shape, res=1000, crs = crs(data))
## interpolate() throws an error because of issue with gstat
idw <- raster::interpolate(r, gs)
idwr <- mask(r, gs)
plot(idwr)
}
col_names <- c("v1", "v2", "v3")
lapply(col_names, function(x) {
gstat_apply(col_name = x, data = data, shape = shape)
}
Error in predict.gstat(model, blockvals, debug.level = debug.level, ...) :
too many spatial dimensions: 18
In addition: Warning message:
In predict.gstat(model, blockvals, debug.level = debug.level, ...) :
NAs introduced by coercion