通过产生NA来捕捉错误 [英] Catch an error by producing NA

查看:98
本文介绍了通过产生NA来捕捉错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前使用

  try.test< -try(results< -lm(log(0)〜1))
if(class(try.test)==try-error ){results< -NA}

我也试过玩tryCatch。



我想找到一个单一的函数/线解决方案。

解决方案

尝试

  result<  -  tryCatch(lm(log(0)〜1),error = function(err)NA)

但这会捕获所有错误,而不仅仅是日志(0)中的错误。


I wish to get NA when a function returns an Error rather than the code halting.

I currently use

try.test<-try(results<-lm(log(0)~1))
if(class(try.test)=="try-error"){results<-NA}

I also tried playing with tryCatch.

I would like to find a single function/line solution.

解决方案

Try

result <- tryCatch(lm(log(0)~1), error=function(err) NA)

But this catches all errors, not just those from log(0).

这篇关于通过产生NA来捕捉错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆