R:计算时间序列中的增量
我在 R 中有一个时间序列的样本:
> str(d)
'data.frame': 5 obs. of 3 variables:
$ date: POSIXct, format: "2010-03-04 20:47:00" "2010-03-04 21:47:00" ...
$ x : num 0 10 11 15.2 20
$ y : num 0 5 7.5 8.4 12.5
> d
date x y
1 2010-03-04 20:47:00 0.0 0.0
2 2010-03-04 21:47:00 10.0 5.0
3 2010-03-04 22:47:00 11.0 7.5
4 2010-03-04 23:47:00 15.2 8.4
5 2010-03-05 00:47:00 20.0 12.5
在此示例中,x 和 y 的样本每小时采集一次(但时间增量不固定)。 x 和 y 值始终在增长(就像汽车中的里程计数器)。我需要三角洲, 两者之间的增长是多少,像这样:
1 2010-03-04 20:47:00 0.0 0.0
2 2010-03-04 21:47:00 10.0 5.0
3 2010-03-04 22:47:00 1.0 2.5
4 2010-03-04 23:47:00 4.2 0.9
5 2010-03-05 00:47:00 4.8 4.1
我还需要每次的增量(x 和 y 增量,除以时间 - 每小时增量)
我将如何在 R 中执行此操作?
I have a timeseries of samples in R:
> str(d)
'data.frame': 5 obs. of 3 variables:
$ date: POSIXct, format: "2010-03-04 20:47:00" "2010-03-04 21:47:00" ...
$ x : num 0 10 11 15.2 20
$ y : num 0 5 7.5 8.4 12.5
> d
date x y
1 2010-03-04 20:47:00 0.0 0.0
2 2010-03-04 21:47:00 10.0 5.0
3 2010-03-04 22:47:00 11.0 7.5
4 2010-03-04 23:47:00 15.2 8.4
5 2010-03-05 00:47:00 20.0 12.5
In this example samples for x and y are taken every hour (but the time delta is not fix).
The x and y values are always growing (like a milage counter in a car). I need the deltas,
how much was the growth in between, something like this:
1 2010-03-04 20:47:00 0.0 0.0
2 2010-03-04 21:47:00 10.0 5.0
3 2010-03-04 22:47:00 1.0 2.5
4 2010-03-04 23:47:00 4.2 0.9
5 2010-03-05 00:47:00 4.8 4.1
And I also need the deltas per time (x and y delta, divided by the time - delta per hour)
How would I do this in R?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦切换到时间感知数据结构,例如 zoo,只需使用
diff()
:您可以轻松地将第一行填充回来,合并,...等 - 请参阅包 动物园了解详情。
Just use
diff()
once switched to a time-aware data structure like zoo:You can easily pad the first row back, merge, ... etc -- see the excellent documentation for package zoo for details.