如何更改 R 中的时间序列(XTS 或 ZOO)? [英] How can I alter a time series (XTS or ZOO) in R?

查看:51
本文介绍了如何更改 R 中的时间序列(XTS 或 ZOO)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 stackoverflow 的新手,也是 R 的新手,但我已经搜索了很长时间,但找不到以下问题的答案.

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.

我有许多数据文件是温度与时间序列的关系.我将 CSV 作为 ZOO 对象导入,然后转换为 XTS.一个正确的文件看起来像这样,一个小时和一个半小时的读数:

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

但有些时间值略有出入 - 即 23:59:00 不是 00:00:00,或 00:29:00 而不是 00:30:00.

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.

我想要一个可以说将时间序列向前移动 1 分钟,但不要更改数据列(例如 S_21)"的命令.我对 gsub() 进行了一些更简单的更改,并且在将数据转换为 ZOO 或 XTS 之前考虑了一个复杂的正则表达式来更改数据.我已经阅读了 lag()diff() 但它们似乎移动了相对于时间序列的数据值;如果我错了,请纠正我.

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.

推荐答案

尝试

index(master21) <- index(master21) + 60    # adds a minute

这将在时间索引上增加一分钟.然后您可以使用 merge() 作为时间戳对齐.

which will add a minute to the time index. You can then use merge() as the timestamps align.

更一般地说,zoo 包的小插图对您也很有用.

More generally, the vignettes of the zoo package will be useful for you too.

这篇关于如何更改 R 中的时间序列(XTS 或 ZOO)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆