I have a nested for loop
that needs to be able to skip to the next iteration in the event of an error.
The error is happening in the inner most loop.
I tried using try_catch
, but I'm met with an error. How should I approach this? Maybe my logic for the for loop is not correctly aligned with try_catch
.
Here is my attempt at trying to skip to the next iteration in the inner most loop.
df <- data.frame(
name = c("Neo","Mr.Smith","Morpheus"),
age = c(22,'2',28)
)
youth <- df$age
for(i in df$name){
for (j in youth){
## error handler
tryCatch({
# error here
if(!is.numeric(j)) {
stop("Not numeric", j)
} else {
print(j)
}
}, error = function(e) {
next
})
}
print(i)
}