简体   繁体   中英

How do I store data from getSymbols (quantmod library) to a list?

Here's the code I'm running

library(quantmod)
library(tseries)
Stocks={}
companies=c("IOC.BO","BPCL.BO","ONGC.BO","HINDPETRO.BO","GAIL.BO")
for(i in companies){
   Stocks[i]=getSymbols(i)
}

I'm trying to get a list of dataframes that are obtained from getSymbols to get stored in Stocks . The problem is that getSymbols directly saves the dataframes to the global environment Stocks only saves the characters in companies in the list.

How do I save the dataframes in the Global Environment to a list?

Any help is appreciated.. Thanks in advance!

Another option is lapply

library(quantmod)
Stocks <- lapply(companies, getSymbols, auto.assign = FALSE)
Stocks <- setNames(Stocks, companies)

from ?getSymbols

auto.assign : should results be loaded to env If FALSE, return results instead. As of 0.4-0, this is the same as setting env=NULL. Defaults to TRUE


Using a for loop you could do

companies <- c("IOC.BO", "BPCL.BO", "ONGC.BO", "HINDPETRO.BO", "GAIL.BO")
Stocks <- vector("list", length(companies))

for(i in seq_along(companies)){
  Stocks[[i]] <- getSymbols(name, auto.assign = FALSE)
}
Stocks

在我的 quantmod (0.4.0) 版本中,有必要在函数的参数中设置env=NULL ,然后返回整个数据帧

使用以下参数作为 getSymbols(i, auto.assign=FALSE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM