如何使用 getsymbols 函数删除每天从雅虎财经下载的数据中的 NA?

How can I delete NA´s within data downloaded daily from Yahoo Finance with the use of getsymbols function?

Underlyings <- c ("AMZN", "ALV.DE", "BMW.DE")
getsymbols(Underlyings, from = "", to = "")

现在有一个用于消除现有 NA 的 if 或 for 循环吗?

你可以做的是使用 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 包。