rtime-seriesxtszooquantmod

How to round trip (to and from a .csv file) with an xts object?


Trying to do a round trip (write/read) with an xts object. read.zoo() returns a data frame! Converting this to an xts fails.

How do I make it an xts object?

library(quantmod)
from <- "2016-01-01" ## leap year
to <- "2017-01-01"
symbol <- "AAPL"
getSymbols(symbol, from=from, to=to)
class(AAPL)
write.zoo(AAPL, file="newapple.txt", sep=",")
x <- read.zoo("newapple.txt")
class(x)
x <- as.xts(x, RECLASS=TRUE) ## has errors
# write.zoo(x, file="newerapple.txt", sep=",") ## crashes

Solution

  • This works for me using the latest version of quantmod (0.4.25), xts (0.13.1) and zoo (1.8.12) on CRAN and R 4.3.1 on Windows.

    library(quantmod)
    
    getSymbols("AAPL")
    write.zoo(AAPL, file = "newapple.csv", sep = ",")
    z <- read.csv.zoo("newapple.csv")
    x <- as.xts(z)
    write.zoo(x, "newapple2.csv", sep = ",")