匹配不同长度的时间向量:一个棘手的问题
我有两组来自不同机器的测量结果。它们随着时间的推移进行测量,时间间隔略有不同 - 例如,一个每 5 分钟进行一次测量,而另一个每 3 分钟进行一次测量。优点是每 5 分钟计算一次,作为整个时间间隔的平均值,因此这些值应该大致对应。我想通过每 5 分钟(光)测量一次来扩展向量,以便其值与每 5 分钟进行的测量向量中的值大致同步。然后应该用前面的值填充间隙。
的示例
Date Light
26/05/2011 16:00 -529.98
26/05/2011 16:05 -276.68
26/05/2011 16:10 -179.63
26/05/2011 16:15 -385.57
26/05/2011 16:20 -1273.6
26/05/2011 16:25 -1109.7
这是每 5 分钟的数据和每 3 分钟的数据
Date Flux
26/05/2011 16:01 0.64
26/05/2011 16:04 -1.96
26/05/2011 16:07 -0.51
26/05/2011 16:10 -1.34
26/05/2011 16:13 -1.28
26/05/2011 16:15 -0.22
。我也不应该认为光测量的矢量(每 5 分钟)比每 3 分钟的矢量短。因此,目标是使 5 分钟测量的向量与 3 分钟向量的长度相同。
我意识到这是一个相当棘手的问题,但任何建议都会受到极大的欢迎。
I have two sets of measurements from different machines. They are measured over time, at slightly different intervals - e.g. one makes a measurement every 5 mins, but the other, every 3 mins. The advantage is that the one every 5 mins is computed as an average over the whole interval so the values should correspond roughly to one another. I would like to expand the vector with measurements every 5 minutes (Light) so that its values are roughly synchronous with the values in vector of measurements made every 5 minutes. The gap should then be filled with the preceding value
Here is an example of the data every 5 minutes
Date Light
26/05/2011 16:00 -529.98
26/05/2011 16:05 -276.68
26/05/2011 16:10 -179.63
26/05/2011 16:15 -385.57
26/05/2011 16:20 -1273.6
26/05/2011 16:25 -1109.7
and the data every 3 minutes
Date Flux
26/05/2011 16:01 0.64
26/05/2011 16:04 -1.96
26/05/2011 16:07 -0.51
26/05/2011 16:10 -1.34
26/05/2011 16:13 -1.28
26/05/2011 16:15 -0.22
I should also not that the vector of light measurement (every 5 mins) is shorter than the vector every 3 minutes. The goal is thus to make the vector of 5 min measurements the same length as the 3 minute vector.
I realise that this is quite a tricky problem, but any suggestions would be greatfuly received.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确的话,这可以通过 Zoo 或 xts 轻松完成。首先,这是您的示例数据:
现在我们加载 xts 包,该包还加载 Zoo.xts 包。然后我们将
Light
和Flux
data.frame 对象转换为 xts 对象。这是很棒的部分。
merge.xts
和merge.zoo
将按索引对齐每个系列。na.locf
使用之前的值填充每个NA
。最后,我们可以从合并的 Data 对象中提取 3 分钟索引。
If I understand correctly, this is easily accomplished with either zoo or xts. First, here's your sample data:
Now we load the xts package, which also loads zoo. Then we convert the
Light
andFlux
data.frame objects to xts objects.Here's the awesome part.
merge.xts
andmerge.zoo
will align each series by index.na.locf
fills in eachNA
with the previous value.Finally, we can extract the 3 minute index from the merged
Data
object.您可以使用大约,这将在数据点之间线性插值。这是一个简单的示例:
要指定要插值的点数量,可以使用 xout 参数,如下所示:
对于更多插值点:
对于您的特定示例,您需要执行诸如插值的 x,y 值之类的操作两个函数都使用两台机器的时间点的交集。这是一个建议:
然后,您可以使用此 x_interp 作为 xout 在两台机器的点之间进行插值:
如果您想获得一个为任意输入插值的函数,请参阅名为 approxfun。
You can use approx, which will linearly interpolate between your datapoints. Here's a quick example:
To specify how many points you want to interpolate, you can use the xout parameter, like this:
For more interpolation points:
For your specific example, you'd want to do something like interpolating the x,y values of both functions using the intersection of timepoints from both machines. Here is one suggestion:
Then, you can use this x_interp as an xout to interpolate between points from both machines:
If you'd like to get a function that interpolates values for arbitrary inputs, see the related function called approxfun.