在R循环中使用tryCatch

在R循环中使用tryCatch

本文介绍了在R循环中使用tryCatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Yahoo获取的数据中读取价格并计算收益,传递无法读取数据的符号。

$ b pre> 库(TTR)
源(util.r)
symbols =
for(sym in c(CSI,XCSIX,IGI)){
cat(\\\
reading data for,sym,\\\

tryCatch(
{
stk = getYahooData(sym,start = 20000101,end = 20200101,freq =daily)
ndays = length(index(stk))
logret = ((log(stk $ Close) - lag(log(stk $ Close))))[2:ndays]
print(summary(logret))},
error =数据,sym))
}

不起作用,给出输出

 读取CSI 
的数据无法读取CSI Index的数据关闭
最小。 :2000-01-04 00:00:00 :-0.1640284
1st Qu.:2003-10-27 06:00:00 1st Qu.:-0.0028756
中位数:2007-08-16 12:00:00中位数:0.0000000
平均值:2007-08-16 05:37:08平均值:0.0003147
第3季:2011-06-05 06:00:00第3季度:0.0037004
最大:2015-03-26 00:00:00 :0.2523210

为XCS​​IX读取数据
无法读取tryCatchOne中的XCSIXError数据(expr,names,parentenv,handlers [[1L]]):
尝试应用函数
调用:tryCatch - > tryCatchList - > tryCatchOne
另外:警告消息:
在文件(文件rt)中:无法打开:HTTP状态为'404未找到'
执行停止

如何正确使用tryCatch?

解决方案

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ sym in c(CSI,XCSIX,IGI)){
cat(\\\
reading data for,sym,\\\

tryCatch(
{
stk = getYahooData(sym,start = 20000101,end = 20200101,freq =daily)
ndays = length(index(stk))
logret =((log(stk $关闭) - 滞后(log(stk $ Close))))[2:ndays]
print(summary(logret))},
error = function(err){
cat数据不存在于公司:,sym,错误是,conditionMessage(err),\\\

})
}


I want to read prices and compute returns for data obtained from Yahoo, passing over symbols for which data could not be read. The code

library("TTR")
source("util.r")
symbols =
for (sym in c("CSI","XCSIX","IGI")) {
    cat("\nreading data for",sym,"\n")
    tryCatch(
    {
    stk = getYahooData(sym, start = 20000101, end = 20200101, freq = "daily")
    ndays = length(index(stk))
    logret = ((log(stk$Close) - lag(log(stk$Close)))) [2:ndays]
    print(summary(logret))},
    error = cat("could not read data for ",sym))
}

does not work, giving output

reading data for CSI
could not read data for  CSI     Index                         Close
 Min.   :2000-01-04 00:00:00   Min.   :-0.1640284
 1st Qu.:2003-10-27 06:00:00   1st Qu.:-0.0028756
 Median :2007-08-16 12:00:00   Median : 0.0000000
 Mean   :2007-08-16 05:37:08   Mean   : 0.0003147
 3rd Qu.:2011-06-05 06:00:00   3rd Qu.: 0.0037004
 Max.   :2015-03-26 00:00:00   Max.   : 0.2523210

reading data for XCSIX
could not read data for  XCSIXError in tryCatchOne(expr, names, parentenv, handlers[[1L]]) :
  attempt to apply non-function
Calls: tryCatch -> tryCatchList -> tryCatchOne
In addition: Warning message:
In file(file, "rt") : cannot open: HTTP status was '404 Not Found'
Execution halted

How do I use tryCatch properly?

解决方案
# This may work
library("TTR")
    source("util.r")
    symbols =
      for (sym in c("CSI","XCSIX","IGI")) {
        cat("\nreading data for",sym,"\n")
        tryCatch(
          {
            stk = getYahooData(sym, start = 20000101, end = 20200101, freq = "daily")
            ndays = length(index(stk))
            logret = ((log(stk$Close) - lag(log(stk$Close)))) [2:ndays]
            print(summary(logret))},
          error=function(err) {
            cat("Data doesn't exist for company:", sym, "and the error is", conditionMessage(err), "\n")
      })
      }

这篇关于在R循环中使用tryCatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:41