如何跳过循环中的错误 [英] How to skip an error in a loop
问题描述
我想跳过一个循环中的错误(如果有),并继续下一个迭代。我想用{0,1,2}随机抽取的元素来计算2×2矩阵的100个逆矩阵。可以有一个单数矩阵(例如,
I want to skip an error (if there is any) in a loop and continue the next iteration. I want to compute 100 inverse matrices of a 2 by 2 matrix with elements randomly sampled from {0, 1, 2}. It is possible to have a singular matrix (for example,
1 0
2 0
这是我的代码
set.seed(1)
count <- 1
inverses <- vector(mode = "list", 100)
repeat {
x <- matrix(sample(0:2, 4, replace = T), 2, 2)
inverses[[count]] <- solve(x)
count <- count + 1
if (count > 100) break
}
在第三次迭代中,矩阵是奇异的,代码停止运行,并显示一条错误消息。实际上,我想绕过这个错误并继续下一个循环,我知道我需要使用 try
或 tryCatch
函数,但我不知道如何使用它们,在这里也提到了类似的问题,但它们都非常复杂,答案远远超出了我的理解,如果有人可以给我一个完整的代码专门针对这个问题,我真的很感激。
At the third iteration, the matrix is singular and the code stops running with an error message. In practice, I would like to bypass this error and continue to the next loop. I know I need to use a try
or tryCatch
function but I don't know how to use them. Similar questions have been asked here, but they are all really complicated and the answers are far beyond my understanding. If someone can give me a complete code specifically for this question, I really appreciate it.
对于单数矩阵,这将把 NULL
s转换为 inverses
This would put NULL
s into inverses
for the singular matrices:
inverses[[count]] <- tryCatch(solve(x), error=function(e) NULL)
如果调用 tryCatch
中的第一个表达式引发一个错误,它执行并返回提供给其错误
参数的函数的值。提供给错误
arg的函数必须将错误本身作为参数(这里我称之为 e
),但是您不需要做任何事情。
If the first expression in a call to tryCatch
raises an error, it executes and returns the value of the function supplied to its error
argument. The function supplied to the error
arg has to take the error itself as an argument (here I call it e
), but you don't have to do anything with it.
然后,您可以将 NULL
条目与逆[! is.null(inverses)]
。
You could then drop the NULL
entries with inverses[! is.null(inverses)]
.
或者,您可以使用较低级别的 try
。
Alternatively, you could use the lower level try
. The choice is really a matter of taste.
count <- 0
repeat {
if (count == 100) break
count <- count + 1
x <- matrix(sample(0:2, 4, replace = T), 2, 2)
x.inv <- try(solve(x), silent=TRUE)
if ('try-error' %in% class(x.inv)) next
else inverses[[count]] <- x.inv
}
如果您的表达式生成错误,尝试
返回一个类 try-error
的对象。如果 silent = FALSE
,它将打印消息。在这种情况下,如果 x.inv
有类 try-error
,我们调用 next
停止执行当前的迭代并移动到下一个,否则我们将 x.inv
添加到 inverses
。
If your expression generates an error, try
returns an object with class try-error
. It will print the message to screen if silent=FALSE
. In this case, if x.inv
has class try-error
, we call next
to stop the execution of the current iteration and move to the next one, otherwise we add x.inv
to inverses
.
您可以避免使用重复
循环与复制
和 lapply
。
You could avoid using the repeat
loop with replicate
and lapply
.
matrices <- replicate(100, matrix(sample(0:2, 4, replace=T), 2, 2), simplify=FALSE)
inverses <- lapply(matrices, function(mat) if (det(mat) != 0) solve(mat))
有趣的是,复制
的第二个参数被视为表达式
,意思是它得到每个重复执行一次。这意味着您可以使用复制
创建从同一表达式生成的任意数量的随机对象的列表
。
It's interesting to note that the second argument to replicate
is treated as an expression
, meaning it gets executed afresh for each replicate. This means you can use replicate
to make a list
of any number of random objects that are generated from the same expression.
这篇关于如何跳过循环中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!