您可以做的是使用lapply 并使用它来调用getSymbols 和na.omit。现在,当您调用 getSymbols 时,该对象将直接放置到您的环境中,并且 na.omit 找不到任何可以执行的操作,但您不会收到警告/错误。如果在使用getSymbols 时使用auto.assign = FALSE,则可以自己分配值,并且可以将getSymbols 的返回结果传递给na.omit。您仍然会收到 SAF.PA 具有空值的警告,但在列表中这些值将被删除。
基于 github 脚本编辑
股票列表中的一个股票 (EI.PA) 出现无法下载的错误。我在函数周围添加了try 来捕捉它,以便它继续下一个股票。
library(quantmod)
underlyings <- c("^STOXX50E", "ALV.DE", "G.MI", "BMW.DE", "SU.PA", "ENI.MI", "IBE.MC", "ORA.PA", "DBK.DE",
"BAYN.DE", "ENEL.MI", "AI.PA", "DTE.DE", "BN.PA", "SAF.PA", "BBVA.MC","PHIA.AS",
"OR.PA", "ASML.AS", "DPW.DE", "AIR.PA", "BNP.PA", "INGA.AS", "ENGI.PA", "ABI.BR",
"EI.PA", "SAN.PA", "CA.PA", "ITX.MC", "MC.PA", "FRE.DE")
my_data <- lapply(underlyings, function(x) try(na.omit(getSymbols(x, from="2016-01-01", to="2019-01-08", auto.assign = FALSE))))
names(my_data) <- underlyings
sapply(my_data, function(x) sum(is.na(x)))
Warning: EI.PA download failed; trying again.
Error : EI.PA download failed after two attempts. Error message:
HTTP error 404.
In addition: Warning messages:
1: ^STOXX50E contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
2: SU.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
3: SAF.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
4: ASML.AS contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
Warning message:
SAN.PA contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.
# show number of empty values
sapply(my_data, function(x) sum(is.na(x)))
sapply(my_data, function(x) sum(is.na(x)))
^STOXX50E ALV.DE G.MI BMW.DE SU.PA ENI.MI IBE.MC ORA.PA DBK.DE BAYN.DE ENEL.MI AI.PA DTE.DE
0 0 0 0 0 0 0 0 0 0 0 0 0
BN.PA SAF.PA BBVA.MC PHIA.AS OR.PA ASML.AS DPW.DE AIR.PA BNP.PA INGA.AS ENGI.PA ABI.BR EI.PA
0 0 0 0 0 0 0 0 0 0 0 0 0
SAN.PA CA.PA ITX.MC MC.PA FRE.DE
0 0 0 0 0
从列表中删除错误:
my_data[which(sapply(my_data, function(x) inherits(x, "try-error")) == TRUE)] <- NULL
# to create one big xts object:
my_big_xts <- Reduce(cbind, my_data)
但如果您想在一个整洁的 data.frame 中有多个股票代码,您可能需要查看 tidyquant 包。