Working with errors inside the loop in R

7

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

The issue is that eventually any of these start values generate error in the gnls routine.

What I need to do is to cause the loop to return with the next value in the event of an error, ignoring the problem.

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

    
asked by anonymous 02.03.2015 / 15:46

1 answer

2

Explain briefly the suggestion of djas (I put it as community wiki).

One way to solve your problem is to use try() or tryCatch() .

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

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

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

for(i in 1:10){
  try(i + "a")
  print(i)
}
    
16.04.2015 / 15:17