How can I alter a time series (XTS or ZOO) in R?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 07:28:33

问题


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.


回答1:


Try

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

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.



来源:https://stackoverflow.com/questions/3224817/how-can-i-alter-a-time-series-xts-or-zoo-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!