在 R 中使用 tryCatch() 在循环中分配错误值 [英] using tryCatch() in R to assign error values in loop
本文介绍了在 R 中使用 tryCatch() 在循环中分配错误值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为 R 中的 tryCatch() 指令苦苦挣扎.我正在尝试获取股票代码的收盘价.
I am struggling with the instructions for tryCatch() in R. I'm trying to capture the closing price for a ticker.
案例 2 = 好案例 = TickersJuly2 = 独特的价格与股票代码的关系
Case 2 = good case = TickersJuly2 = unique price to ticker relationship
情况 1 = 坏情况 = TickersJuly1 = FABU 收盘价是 CETX 的重复
Case 1 = bad case = TickersJuly1 = FABU close price is the repeat of CETX
案例 1 所需的输出是 0,表示 FABU.
Case 1 desired output is a 0 for FABU.
library(TTR)
close.price1=NULL
TickersJuly1 <- c('DIT','CETX','FABU')
TickersJuly2<- c('AAPL','A','AA')
for(i in TickersJuly1){
tryCatch(close <- getYahooData(i,20150727,20150727,'daily',"price"),
error = function(e) close$Close <- 0,
warning = function(w) close$Close <- 0,
finally = function(f) close$Close <- 0)
close.price <- c(as.character(close$Close),i)
close.price1 <- rbind(close.price1,close.price)
}
推荐答案
我认为这有效.您应该将 tryCatch
的结果分配给一个变量.
I think this works. You should be assigning the result of the tryCatch
to a variable.
for(i in TickersJuly1){
close <- tryCatch(
getYahooData(i,20150727,20150727,'daily',"price"),
error = function(e) list(Close=0),
warning = function(w) list(Close=0),
finally = function(f) list(Close=0))
close.price <- c(as.character(close$Close),i)
close.price1 <- rbind(close.price1,close.price)
}
这篇关于在 R 中使用 tryCatch() 在循环中分配错误值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文