Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield
Thanks again Joris - you've been very helpful J From: Joris FA Meys [via R] [mailto:ml-node+2267176-1824205151-120...@n4.nabble.com] Sent: 24 June 2010 16:40 To: Paul Chatfield Subject: Re: How to say "if error" You could do that using the options, eg : set.seed(1) x <- rnorm(1:10) y

Re: [R] How to say "if error"

2010-06-24 Thread Duncan Murdoch
On 24/06/2010 11:12 AM, Paul Chatfield 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 incorpo

Re: [R] How to say "if error"

2010-06-24 Thread Joris Meys
You could do that using the options, eg : set.seed(1) x <- rnorm(1:10) y <- letters[1:10] z <- rnorm(1:10) warn <-getOption("warn") options(warn=2) for (i in list(x,y,z)){ cc <- try(mean(i), silent=T) if(is(cc,"try-error")) {next} print(cc) } options(warn=warn) see ?options under "warn" C

Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield
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

Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield
That's great. That solves it. I can work on eloquence later :) I just to sort out that model problem: dof<-numeric(10) for (i in 1:10){ x<-rnorm(i-1);y<-rnorm(i-1) cc <- try(lm(y~x), silent=T) if(is(cc,"try-error")) {dof[i]<-NA} else {dof[i]<-(summary(lm(rnorm(10)~rgamma(10,1,1)))$co

Re: [R] How to say "if error"

2010-06-24 Thread Joris Meys
An old-fashioned and I guess also advised-against method would be to use try() itself, eg : set.seed(1) x <- rnorm(1:10) y <- letters[1:10] z <- rnorm(1:10) for (i in list(x,y,z)){ cc <- try(sum(i), silent=T) if(is(cc,"try-error")) {next} print(cc) } Put silent=F if you want to see the err

Re: [R] How to say "if error"

2010-06-24 Thread Duncan Murdoch
On 24/06/2010 7:06 AM, Paul Chatfield wrote: I've had a look at the conditions in base and I can't get the ones to work I've looked at but it is all new to me. For example, I can work the examples for tryCatch, but it won't print a finally message for me when I apply it to my model. Even if

Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield
Thanks Roman - you're right it can do more than I thought. We're close now to solving it I feel. Essentially I'm trying to get the code below to work. If the condition is satisfied, it prints 2, but it doesn't save it in z. I want it to save it even though there's an error. Perhaps you can ea

Re: [R] How to say "if error"

2010-06-24 Thread Paul Chatfield
I've had a look at the conditions in base and I can't get the ones to work I've looked at but it is all new to me. For example, I can work the examples for tryCatch, but it won't print a finally message for me when I apply it to my model. Even if I could get this to work, I think it would stil

Re: [R] How to say "if error"

2010-06-24 Thread Roman Luštrik
Does ?tryCatch do what you want? -- View this message in context: http://r.789695.n4.nabble.com/How-to-say-if-error-tp2266619p2266749.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.

Re: [R] How to say "if error"

2010-06-24 Thread Roman Luštrik
I'm sorry, I don't understand your problem to the detail so my suggestion may be waaay off, but how's this? You can see in vector vec - all ok values except where there's an error. n <- 10 vec <- rep(NA, n) for (j in 1:n) {tryCatch(ifelse(j==5, vec[j] <- j, j), finally=print("oh dear"))} vec Ch

[R] How to say "if error"

2010-06-24 Thread Paul Chatfield
Hi - I am looping over a structural equation model for a variety of datasets. Occasionally, the model returns an error and the loop then breaks. I'd like to set a condition which says something like "if error, then print NAs" rather than the loop breaking, but I don't know how to say "if error".