如何更改R中的时间序列(XTS或ZOO)?

phr*_*uin 9 r time-series zoo xts

我是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
Run Code Online (Sandbox Code Playgroud)

但有些人的时间价值略有偏差 - 即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
Run Code Online (Sandbox Code Playgroud)

我想纠正这些时间序列,因为微小差异对我的分析并不重要,我最终想要合并文件,因此每个时间序列需要具有相同的时间.

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.

Dir*_*tel 11

尝试

index(master21) <- index(master21) + 60    # adds a minute
Run Code Online (Sandbox Code Playgroud)

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

更一般地说,zoo包装的小插曲对你也很有用.