Search code examples
rtime-seriesxtsquantmod

How to reverse chronological order with getSymbols in R


I download some stock data with quantmod and retrieve the closing prices:

require(quantmod)
tickers<-c('AAPL','GOOGL')
getSymbols(tickers, from="2014-03-01")
close <- do.call(merge, lapply(tickers, function(x) Cl(get(x))))
head(close)

AAPL.Close GOOGL.Close
2014-03-03     527.76     1202.69
2014-03-04     531.24     1214.91
2014-03-05     532.36     1218.26
2014-03-06     530.75     1219.61
2014-03-07     530.44     1214.79
2014-03-10     530.92     1211.57

Is there a way to run getSymbols so that the most recent dates output is first?


Solution

  • The final result is the xts object. xts is "fanatic" about order. But you can access the data with function coredata (for data part) and time for time vector.

    Try for example:

    res <- data.frame( time = time(close), coredata(close))
    res <- res[nrow(res):1,]