Work with errors within R loop

Asked

Viewed 339 times

7

I entered the non-linear adjustment function gnls within a loop for so that it could test a series of start values automatically.

The point is that eventually some of these start values create a routine error gnls.

What I need is to get, in case of error, the loop starts with the next value, ignoring the problem.

Something like on error goto next, but there is no such syntax in R.

  • 6

    see ?tryCatch()

  • Thank you so much, @djas! It’s working!

  • 3

    @djas put your comment as a reply, abs

1 answer

2


Briefly explaining the djas suggestion (I put as community wiki).

One way to solve your problem is by using the try() or tryCatch().

For example the code below will stop at i+"a" because of the error and will not perform the print:

for(i in 1:10){
  i + "a"
  print(i)
}

Putting try() the code continues even with the error.

for(i in 1:10){
  try(i + "a")
  print(i)
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.