[英]Clean way to interpolate row data within a dataframe with R
I have the following dataframe, let's call it DF: 我有以下数据框,我们称其为DF:
DF <- data.frame(
x = c(0, 2, 4, 6, 8),
y = c(1, 2, 3, 4, 5)
)
I would like to know if there is an easy and clean way to end up with the following dataframe, let's call it DF_target from DF: 我想知道是否有一种简单干净的方法来结束以下数据框,从DF中将其称为DF_target:
DF_target <- data.frame(
x = c(0, 1, 2, 3, 4, 5, 6, 7, 8),
y = c(1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5)
)
I would like to use linear interpolation between the rows. 我想在行之间使用线性插值。
So basically something like: 所以基本上像这样:
function(DF) -> DF_target
The example has been simplified. 该示例已简化。
This is what approx
was built to do. 这是
approx
要执行的操作。
approx(DF$x, DF$y, n=9)
$x
[1] 0 1 2 3 4 5 6 7 8
$y
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
which returns (from ?approx
) 返回(从
?approx
)
a list of linearly interpolated data points.
线性插值数据点的列表。
To make it a data.frame, just wrap the function in data.frame
. 要使其成为data.frame,只需将函数包装在
data.frame
。
data.frame(approx(DF$x, DF$y, n=9))
x y
1 0 1.0
2 1 1.5
3 2 2.0
4 3 2.5
5 4 3.0
6 5 3.5
7 6 4.0
8 7 4.5
9 8 5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.