[R] how to avoid a for looping break after an error message

cls59 chuck at sharpsteen.net
Sat Jul 25 20:49:36 CEST 2009




Victor Landeiro wrote:
> 
> Hi all,
> I wrote a piece of code that generates simulated variables. after variable
> generation I use them in several analyzes.
> However, when I use a for to repeat the procedure 1000 times I get an erro
> message in one of the "for" steps, precisely at this time:
> gls.temp<- gls(y2 ~ x2,correlation=corExp(form=~coord2[,1]+coord2[,2]))  #
> coord 2 are spatial coordinates
> and the error message is:
> Error in gls(y2 ~ x2, correlation = corExp(form = ~coord2[, 1] + coord2[,
> :  false convergence (8)
> I don´t know what causes this error. My problem is that this error stops
> all
> the calculations during the for looping and all previous results are lost.
> Can anyone help me to avoid that my looping stops after the error message?
> Thanks in advance,
> Victor Landeiro
> 
> <snip>
> 
> 

You could wrap the offending function in the try statement:

gls.temp <- try(  gls(y2 ~
x2,correlation=corExp(form=~coord2[,1]+coord2[,2]))  )

Then you want to check the class of gls.temp- if it is a "try-error", the
data you are expecting will not be there:

if( class(gls.temp) == 'try-error' ){

  # You had an error, maybe just skip this iteration using next

}else{

  # It's all good- do your normal calculations  

}

Hope that helps!

-Charlie

-----
Charlie Sharpsteen
Undergraduate
Environmental Resources Engineering
Humboldt State University
-- 
View this message in context: http://www.nabble.com/how-to-avoid-a-for-looping-break-after-an-error-message-tp24660446p24660575.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list