如何使用 R 插值空间值?

问题描述 投票:0回答:1

我想在栅格中插入缺失值:

r <- rast(xmin=0, xmax=10, ymin=0, ymax=10, nrows=50, ncols=50)
e =  rast(xmin=2, xmax=4, ymin=2, ymax=4, nrows=10, ncols=10)
values(r) <- 1:ncell(r)
values(e) <- 1:ncell(e)

r=raster(r)
e=raster(e)
r[intersect(r,e)]=NA
plot(r)

我该怎么做?

missing values within raster

我知道如何使用不同的方法(样条线、克里金法、IDW...)在点之间进行插值,但我不知道如何在多边形尺度上执行此操作。

r interpolation raster spatial
1个回答
1
投票

由于这是二维数据,您可以将通常使用的相同插值函数应用于矩阵的每一列。例如,使用

approx()
:

r_interp <- r |>
    as.matrix() |>
    apply(MARGIN = 2, \(x) approx(x)[["y"]]) |>
    raster()

plot(r_interp)

您可能认为这仅适用于 x 轴上的每个值都是相同的,但它在其他情况下也适用。例如,如果我们生成一个不同的矩阵并在明显的非线性区域添加更多

NA
值:

Z  <- as.matrix(r)
r_nonlin <- sapply(
    seq_len(ncol(Z)),
    \(i) Z[, i] * i
) |>
    raster()
# Create more NAs
r_nonlin[30:40, 30:40] <- NA

plot(r_nonlin)

我们仍然可以使用相同的方法:

r_nonlin |>
    as.matrix() |>
    apply(MARGIN = 2, \(x) approx(x)[["y"]]) |>
    raster() |>
    plot()

© www.soinside.com 2019 - 2024. All rights reserved.