如果错误,则在R中的for循环中进行下一次迭代

时间:2016-09-07 14:31:56

标签: r for-loop error-handling try-catch next

如果for循环错误中的操作,我正在寻找一种简单的方法来继续在R中的for循环中进行下一次迭代。

我在下面重新创建了一个简单的案例:

for(i in c(1, 3)) {
  test <- try(i+1, silent=TRUE)
  calc <- if(class(test) %in% 'try-error') {next} else {i+1}
  print(calc)
}

这正确地给了我以下计算值。

[1] 2
[1] 4

但是,一旦我将i中的向量更改为包含非数字值:

for(i in c(1, "a", 3)) {
  test <- try(i+1, silent=TRUE)
  calc <- if(class(test) %in% 'try-error') {next} else {i+1}
  print(calc)
}

此for循环不起作用。我希望得到与上面相同的计算值,其中向量不包括i中的非数字值。

我尝试使用tryCatch,如下所示:

for(i in c(1, "a", 3)) {
  calc <- tryCatch({i+1}, error = function(e) {next})
  print(calc)
}

但是,我收到以下错误:

Error in value[[3L]](cond) : no loop for break/next, jumping to top level 

有人可以帮我理解如何使用R中的for循环来实现这个目标吗?

2 个答案:

答案 0 :(得分:1)

正如Dason所说,原子矢量确实不是存储混合数据类型的最佳方式。列表就是为了这个。请考虑以下事项:

{{1}}

换句话说,你的前循环“有效”。只是总是失败并进入下一次迭代。

答案 1 :(得分:0)

这是使用&#34; purr&#34;的解决方案。包可能会有所帮助。 它遍历您的列表或向量并返回将导致错误的元素

#Wrap the function you want to use in the adverb "safely" 
safetest <- safely(function(x){ifelse(is.na(as.numeric(x)),
                                  x+1,
                                  as.numeric(x)+1)})

myvect<-c(1,"crumbs",3) #change to list if you want a list

#Use the safe version to find where the errors occur
check <- myvect  %>% 
  map(safetest) %>%
  transpose %>% .$result %>% 
  map_lgl(is_null)

myvect[check]

#This returns the results that did not through an error
#first remove NULL elements then flatten to double.
#The two flatten expresiion can be replaced by a single unlist
myvect  %>% 
  map(safetest) %>%
  transpose %>% .$result %>% 
  flatten()%>%flatten_dbl()

请参阅https://blog.rstudio.org/2016/01/06/purrr-0-2-0/了解原始示例。