[R] ignore error in for-loop

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Oct 6 15:30:43 CEST 2008


Erich STRIESSNIG wrote:
> Hi all,
>
> Can anybody tell me what to do in case an error occurs inside a
> for-loop and I don't want the program to exit the loop? Rather instead
> I would like it to just go to the next number in the loop and try
> again with the new number. Is there any function like "on error go
> back to" or "skip error" ... ?

there's the keyword 'next' in r which you could use to proceed to the
next value in a for loop.
you can also use try({...}) within the loop, e.g.:

for (i in 0:3)
   try({if (i==2) stop(i); print(i)}, silent=TRUE)

[1] 0
[1] 1
[1] 3

vQ



More information about the R-help mailing list