Merge getSymbols result into one xts object

前端 未结 3 1555
灰色年华
灰色年华 2020-12-03 23:51

I have the following code:

library(quantmod)
tckrs <- c(\"TLT\", \"LQD\", \"HYG\", \"SPY\", \"DBC\")
NumTckrs  <-  length(tckrs)
getSymbols(tckrs, from         


        
3条回答
  •  萌比男神i
    2020-12-04 00:25

    Load all the data into an environment, then call Ad on each, and merge them. Also note that getSymbols returns an xts object by default, therefore your MainDF is an xts object, not a data.frame.

    library(quantmod)
    # create new environment
    myEnv <- new.env()
    # pull all data and load into myEnv
    getSymbols("TLT;LQD;HYG;SPY;DBC", env=myEnv)
    # eapply calls Ad on each symbol in myEnv and returns a list
    # do.call calls merge with each element returned from eapply as an argument
    MainXTS <- do.call(merge, c(eapply(myEnv, Ad),all=FALSE))
    

提交回复
热议问题