I'm using the xts package to create a sample time series in R. I've created a range of dates, separated by the minute, created sample data for each of those dates, and then finally aggregating on the hour, summing the data. This works, except for one issue.
Once I've done the aggregation, the index does not show each data on the hour, but on the 59th minute. I need the index to show on the hour for merging concerns. Below is my code:
#xts simple example code
BD <- chron("01/01/2015", "00:00:00") # Setting begin date.
ED <- chron("02/01/2015", "23:59:00") # Setting end date.
DS <- seq(BD, ED, by = times("00:01:00")) # Creating a sequence of dates seperated by a minute.
data <- runif(length(DS), 0, 100) # Generating random numerical data the length of the date sequence.
x <- xts(data, DS) # Creates an xts object indexed by the dates of "DS" with data from "data".
colnames(x) <- "Data" # Just renaiming the data column in the xts object.
x.agg <- period.apply(x, endpoints(x, "hours"), sum) # Aggregating by hour
I've tried this method:
index(x.agg) <- index(x.agg) - (1/24/60) * 59
but it gives me this response in the tail:
> index(x.agg) <- index(x.agg) - (1/24/60) * 59
> tail(index(x.agg))
[1] (02/01/15 13:00:00) (02/01/15 14:00:00) (02/01/15 15:00:00) (02/01/15
16:00:00) (02/01/15 17:00:00)
[6] (02/01/15 18:00:00)
The whole idea above is to simply subtract by 59 minutes to get it on the hour, but it doesn't seem to work. I've also tried truncating and rounding, but they also give me strange results. Any ideas would be greatly appreciated!