如何更改 R 中的时间序列(XTS 或 ZOO)?
我是 stackoverflow 的新手,对 R 也相当陌生,但经过长时间的艰苦搜索,找不到以下问题的答案。
我有许多数据文件,它们是温度与时间序列的关系。我将 CSV 作为 ZOO 对象导入,然后转换为 XTS。正确的文件如下所示,其中包含整点和半点的读数:
>head(master1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
但有些文件的时间值稍有偏差 - 即 23:59:00 而不是 00:00:00,或者 00:29:00 而不是 00: 30:00。
>head(master21)
S_21
2010-03-04 23:59:00 -0.593
2010-03-05 00:29:00 -0.908
2010-03-05 00:59:00 -1.034
2010-03-05 01:29:00 -1.223
2010-03-05 01:59:00 -1.349
2010-03-05 02:29:00 -1.538
我想纠正这些时间序列,因为微小的差异对我的分析并不重要,而且我最终想要合并文件,因此每个时间序列需要具有相同的时间。
我想要一个可以只说“将时间序列向前移动 1 分钟,但不更改数据列(例如 S_21)”的命令。 我在 gsub()
上获得了一些运气,可以更轻松地进行更改,并考虑使用复杂的正则表达式来更改数据,然后再将其转换为 ZOO 或 XTS。我已经阅读过有关 lag() 和 diff() 的内容,但它们似乎相对于时间序列移动了数据值;如果我错了,请纠正我。
任何解决此问题的帮助将不胜感激。
I am new to stackoverflow and fairly new to R but have searched long and hard and cannot find an answer to the following question.
I have a number of data files that are temperature against a time series. I am importing the CSV as a ZOO object then converting to XTS. A correct file looks like this, with readings on the hour and the half hour:
>head(master1)
S_1
2010-03-03 00:00:00 2.8520
2010-03-03 00:30:00 2.6945
2010-03-03 01:00:00 2.5685
2010-03-03 01:30:00 2.3800
2010-03-03 02:00:00 2.2225
2010-03-03 02:30:00 2.0650
But the time value on some are slightly out - i.e. 23:59:00 not 00:00:00, or 00:29:00 instead of 00:30:00.
>head(master21)
S_21
2010-03-04 23:59:00 -0.593
2010-03-05 00:29:00 -0.908
2010-03-05 00:59:00 -1.034
2010-03-05 01:29:00 -1.223
2010-03-05 01:59:00 -1.349
2010-03-05 02:29:00 -1.538
I want to correct these time series, as the minute difference is not important for my analysis and I ultimately want to merge the files, so each timeseries needs to have the same timing.
I want a command that can just say "shift the time series forward by 1 minute, but don't alter the data column (e.g. S_21).
I have had some luck with gsub()
on easier changes, and contemplated a complex regex to change the data before it is converted to ZOO or XTS. I have read about lag()
and diff()
but they seem to move the data values relative to the time series; please correct me if I am wrong.
Any help solving this issue would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试
将时间索引增加一分钟。然后,您可以使用
merge()
作为时间戳对齐。更一般地说,
zoo
包的小插图对您也很有用。Try
which will add a minute to the time index. You can then use
merge()
as the timestamps align.More generally, the vignettes of the
zoo
package will be useful for you too.