Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

display format of xts when time is 00:00

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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    1
2023-06-14 16:00:00    2
2023-06-14 17:00:00    3
2023-06-14 18:00:00    4
2023-06-14 19:00:00    5
2023-06-14 20:00:00    6
2023-06-14 21:00:00    7
2023-06-14 22:00:00    8
2023-06-14 23:00:00    9
2023-06-15 00:00:00   10

and when printing y I get

y
                        [,1]
2023-06-14 15:00:00    1
2023-06-14 16:00:00    2
2023-06-14 17:00:00    3
2023-06-14 18:00:00    4
2023-06-14 19:00:00    5
2023-06-14 20:00:00    6
2023-06-14 21:00:00    7
2023-06-14 22:00:00    8
2023-06-14 23:00:00    9
2023-06-15 00:00:00   10
2023-06-15 01:00:00   11
2023-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    1
2023-06-14 16:00:00    2
2023-06-14 17:00:00    3
2023-06-14 18:00:00    4
2023-06-14 19:00:00    5
2023-06-14 20:00:00    6
2023-06-14 21:00:00    7
2023-06-14 22:00:00    8
2023-06-14 23:00:00    9
2023-06-15 00:00:00   10
2023-06-15 01:00:00   11
2023-06-15 02:00:00   12

>Solution :

You could change the format and add the timezone like this:

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)

paste0(format(last(index(x)),"%Y-%m-%d %H:%M:%S"), " GMT")
#> [1] "2023-06-15 00:00:00 GMT"

Created on 2023-06-14 with reprex v2.0.2

Note: the output is a character!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading