【问题标题】:How can I delete NA´s within data downloaded daily from Yahoo Finance with the use of getsymbols function?如何使用 getsymbols 功能删除每天从 Yahoo Finance 下载的数据中的 NA?
【发布时间】:2019-01-07 14:20:38
【问题描述】:
Underlyings <- c ("AMZN", "ALV.DE", "BMW.DE")
getsymbols(Underlyings, from = "", to = "")

现在是用于消除现有 NA 的 if 或 for 循环?

【问题讨论】:

  • 通常在数据帧上调用na.omit() 会删除所有缺失值的行。但是,您在示例中给出的所有符号都返回完整的数据,因此很难测试。我跑了getSymbols(Underlyings),三个数据集都完成了。
  • Stack Overflow 要求帖子为英文。你能把你的问题的正文翻译成英文吗?谢谢。
  • 例如,如果您运行 SU.PA 或 SAF.PA,您会得到缺失值显示为 NA 的数据。我尝试了函数 na.omit(getSymbols(Underlyings, from = "", to = "") 但仍然包含 NA。我想也许可以使用 if 或 which 条件来解决这个问题?但我没有在这种情况下不知道怎么写
  • 正文只是问我是否可以使用哪个或如果条件来解决问题,很抱歉我没有翻译它
  • 基本上我的问题是自动完成这项工作,如果我对包含 NA 的单个下载数据使用 na.omit 函数,它可以工作,但我需要一个自动运行它的代码而无需计算找出哪些数据包含 NA,然后使用 na.omit 删除每个数据

标签: r finance yahoo quantmod


【解决方案1】:

您可以做的是使用lapply 并使用它来调用getSymbolsna.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 包。

【讨论】:

  • 非常感谢 Phiver!但是如果我运行这段代码,股票数据不会在我的环境中下载,它在你的工作吗?
  • 股票数据不在全局环境中,但一切都在列表对象my_data中。从那里你可以使用 lapply(或 map)和其他功能来做任何你想做的事情。
  • 如果我运行您提供的第一个代码,然后运行第二个名为 names (my_data)
  • 它总是告诉我使用 auto.assign = TRUE
  • 您是否重新启动了 R 会话?我在多个新会话中运行此代码,一切正常。关于‘getSymbols’ currently uses auto.assign=TRUE by default, but will use auto.assign=FALSE in 0.5-0 的消息你可以忽略。最好使用auto.assign = FALSE,这样您就可以控制正在发生的事情。
猜你喜欢
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多