Search code examples
rxtszoo

dateFormat in as.xts function


I want to convert the dataset "co2" that comes in R to a CSV. My code is the following:

require(xts)
require(zoo)
co2_xts <- as.xts(co2)
write.zoo(co2_xts, file="demo.csv",sep=",")

This works fine. But the time index shows as, for example, "ene. 1959" (it is in spanish). I would like the index to take the format "%B %Y" if possible. If I use:

co2_xts <- as.xts(co2,dateFormat="%B %Y")

This throws an error:

Error in `as.%B %Y`(c(1959, 1959.08333333334, 1959.16666666668, 1959.25000000002,  : 
could not find function "as.%B %Y"

So how can I achieve what I want?


Solution

  • You can adjust the index format after you created the xts with the indexFormat function.

    In this case:

    indexFormat(co2_xts) <- "%B %Y" 
    

    Note that this only changes the display format.