将一维向量插值到 R 中指定的新大小

Interpolate a one-dimensional vector to specified new size in R

我如何插值,假设 R 中的 10 数字 list/vector 到指定的新大小(例如 size=6)?

ls <- c(1:10) # size 10

结果如下:

ls_interp <- ... # interpolate

> ls_interp
[1] 1 2 4 6 8 10

试试这个:

ls_interp <- approx( x, n=6 )$x
ls_interp

它输出:[1] 1.0 2.8 4.6 6.4 8.2 10.0

实际上它会同时返回 x 和 y(但在这种情况下您只需要一个)

另一种选择是使用 seq(但我认为 更好)

> seq(min(ls), max(ls), length.out = 6)
[1]  1.0  2.8  4.6  6.4  8.2 10.0

> min(ls) + (length(ls) - 1) / 5 * (seq(6) - 1)
[1]  1.0  2.8  4.6  6.4  8.2 10.0