插值数据帧的缺失值

时间:2016-01-06 23:09:05

标签: r dataframe interpolation missing-data

我有这样的数据集:

x   y   z
1   1   0.954
1   3   0.134
1   30  0.123
2   1   0.425
2   3   0.123
2   30  0.865
5   1   0.247
5   3   0.654
5   30  0.178

让我们将其视为在4x29场上9点处采样的表面高度。假设我想通过插值填充缺失值(线性很好),这样我最终得到[1,5]中每个(整数)x的z值和[1,30]中的每个y。我希望结果仍然是具有相同结构的数据框。

我怎样才能在R?

中这样做

1 个答案:

答案 0 :(得分:1)

我会把以前缺乏答案作为礼物:)

#akima_0.5-12
library(akima)

my_df <- data.frame(
    x = c(rep(1, 3), rep(2, 3), rep(5, 3)),
    y = rep(c(1, 3, 30), 3),
    z = c(0.954, 0.134, 0.123, 0.425, 0.123, 0.865, 0.247, 0.654, 0.178)
)

my_op <- interp(
    x = my_df$x,
    y = my_df$y,
    z = my_df$z,
    xo = 1:5, # vector of x coordinates to use in interpolation
    yo = 1:30, # vector of y coordinates to use in interpolation
    linear = TRUE # default interpolation method
)

my_op$z # matrix of interpolated z coordinates, (row, col) correspond to (x, y)

ind <- which(!is.nan(my_op$z), arr.ind = TRUE)
desired_output <- data.frame(
    x = ind[, 1],
    y = ind[, 2],
    z = as.vector(my_op$z) # data are organized column-by-column
)