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.
eXtensible Time Series (xts) is a powerful package that provides an extensible time series class, enabling uniform handling of many R time series classes by extending zoo.
Creating a time seriesThe ts() function will convert a numeric vector into an R time series object. The format is ts(vector, start=, end=, frequency=) where start and end are the times of the first and last observation and frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).
merge() joins an xts object to another xts object on the index, or a vector of dates to an xts object (useful to fill in gaps in an xts object). merge() takes three arguments. First is ... , an arbitrary number of objects to combine. Second is join = c("inner", "left", "right", "outer") .
zoo is an R package providing an S3 class with methods for indexed totally ordered observations, such as discrete irregular time series. Its key design goals are independence of a particular index/time/date class and consistency with base R and the "ts" class for regular time series.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With