简体   繁体   中英

How can I alter a time series (XTS or ZOO) in 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.

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 - ie 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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM