[R] How to say "if error"

Joris Meys jorismeys at gmail.com
Thu Jun 24 17:38:24 CEST 2010

You could do that using the options, eg :

x <- rnorm(1:10)
y <- letters[1:10]
z <- rnorm(1:10)

warn <-getOption("warn")
for (i in list(x,y,z)){
  cc <- try(mean(i), silent=T)
  if(is(cc,"try-error")) {next}

see ?options under "warn"


On Thu, Jun 24, 2010 at 5:12 PM, Paul Chatfield
<p.s.chatfield at reading.ac.uk> wrote:
> On a similar issue, how can you detect a warning in a loop - e.g. the
> following gives a warning, so I'd like to set up code to recognise that and
> then carry on in a loop
> x<-rnorm(2);y<-c(1,0)
> ff<-glm(y/23~x, family=binomial)
> so this would be incorporated into a loop that might be
> x<-rnorm(10);y<-rep(c(1,0),5)
> for (i in 1:10)
> {ee<-glm(y~x, family=binomial)
> ff<-glm(y/23~x, family=binomial)}
> from which I would recognise the warning in ff and not those in ee, saving
> results from ee and not from ff. The last bit would be easy adding a line
> if(there_is_a_warning_message) {newvector<-NA} else {use results} but how do
> you detect the warning message?
> Thanks all for your feedback so far,
> Paul
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2267140.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

More information about the R-help mailing list