Search code examples
rdatextsdate-formatting

what options are there for dateFormat in as.xts() in R


When transforming a matrix/dataframe etc into a xts object I often use the function as.xts(). If my initial object has an index containing plain Dates like "2020-06-20", the output of as.xts() will have a timezone added like "2020-06-20 CEST".

I know there is an buildin option dateFormat, but I don't what values I can pass into that. Is there a list of possible inputs somewhere? I can't find any in the documentation the only one I know is "POSIXct"

new_xts_object <- as.xts(my_matrix, dateFormat="POSIXct")

So what else dateFormat's are there? And is there one just like as.Date(), a plain date format?


Solution

  • You find such a list in help("index.xts"):

    The specified value for tclass<- must be a character string containing one of the following: Date, POSIXct, chron, yearmon, yearqtr or timeDate.

    Trying it:

    library(xts)
    library(timeSeries)
    
    x <- timeSeries(1:10, 1:10)
    as.xts(x, dateFormat = "POSIXct")
    as.xts(x, dateFormat = "POSIXlt")
    as.xts(x, dateFormat = "Date")
    
    library(chron)
    as.xts(x, dateFormat = "chron")
    
    library(timeDate)
    as.xts(x, dateFormat = "yearmon")
    as.xts(x, dateFormat = "yearqtr")
    as.xts(x, dateFormat = "timeDate")