Search code examples
rdatetimexts

Formating xts date & time X2020.01.06.06.00.00


I have an xts table all.transactions showing:

structure(list(Quantity = c(0, 162000, 149000, -149000)), row.names = c("X2020.01.06.06.00.00", 
"X2020.01.10.15.00.00", "X2020.02.03.15.00.00", "X2020.02.03.15.00.00.1"
), class = "data.frame")

I noticed that the index of XTS table would tend to change from 2020-01-06 06:00:00 to X2020.01.06.06.00.00 when there are two record with the same time.

Is there a quick way to format/convert it back to normal (2020-01-06 06:00:00) please?


Solution

  • The row names are stored in the attributes of tsx named "row.names" and accessible with attr(). So one way is to format these attributes as time format.

    attr(tsx, "row.names") <- as.character(strptime(attr(tsx, "row.names"), 
                                                    format="X%Y.%m.%d.%H.%M.%S"))
    

    The problem, though, is that time series with duplicate row names are not valid. But maybe this solution is applicable to your real data.


    Data

    tsx <- structure(list(Quantity = c(0, 162000, 149000, -149000)), row.names = c("X2020.01.06.06.00.00", 
    "X2020.01.10.15.00.00", "X2020.02.03.15.00.00", "X2020.02.03.15.00.00.1"
    ), class = "data.frame")