How can I change the format of the return value of last
if the time of that time step is 00:00?
Here is an example
I have two xts
objects containing hourly observations with different end times that I want to align.
library(xts)x <- c(1:10)dt <- seq.POSIXt(from = as.POSIXct("2023-06-14 15:00", tz = "GMT"), by = "hour", length.out = 10)x <- xts(x, order.by=dt)y <- c(1:12)dt <- seq.POSIXt(from = as.POSIXct("2023-06-14 15:00", tz = "GMT"), by = "hour", length.out = 12)y <- xts(y, order.by=dt)
If I print x
I get
x [,1]2023-06-14 15:00:00 12023-06-14 16:00:00 22023-06-14 17:00:00 32023-06-14 18:00:00 42023-06-14 19:00:00 52023-06-14 20:00:00 62023-06-14 21:00:00 72023-06-14 22:00:00 82023-06-14 23:00:00 92023-06-15 00:00:00 10
and when printing y
I get
y [,1]2023-06-14 15:00:00 12023-06-14 16:00:00 22023-06-14 17:00:00 32023-06-14 18:00:00 42023-06-14 19:00:00 52023-06-14 20:00:00 62023-06-14 21:00:00 72023-06-14 22:00:00 82023-06-14 23:00:00 92023-06-15 00:00:00 102023-06-15 01:00:00 112023-06-15 02:00:00 12
This is all good so far. However, if use last
to determine the end of the time series I get a format of the date without time whereas this is not the case when determine the last entry of y
:
last(index(x)) [1] "2023-06-15 GMT"last(index(y))[1] "2023-06-15 02:00:00 GMT"
If I now do the following, y
is not subsetted by the extend of x
, probably because of the time format:
y[paste(first(index(x)), last(index(x)), sep="/")] [,1]2023-06-14 15:00:00 12023-06-14 16:00:00 22023-06-14 17:00:00 32023-06-14 18:00:00 42023-06-14 19:00:00 52023-06-14 20:00:00 62023-06-14 21:00:00 72023-06-14 22:00:00 82023-06-14 23:00:00 92023-06-15 00:00:00 102023-06-15 01:00:00 112023-06-15 02:00:00 12