[R] How to a handle an error in a loop [Broadcast]

Liaw, Andy andy_liaw at merck.com
Sun May 7 03:13:37 CEST 2006


This ought to work:

resultdt <- lapply(PGWide[, 240:389], function(x, ...) try(tdt(x, ...)))

You can then check the class of each component to see which one failed.

Andy 

From: Farrel Buchinsky
> 
> "Berton Gunter" <gunter.berton at gene.com> wrote in message 
> news:008601c67097$de1b46e0$5bc4fea9 at gne.windows.gene.com...
> > ?try
> >
> > as in
> >
> > result<- try (some R expression...)
> > if (inherits(result,'try-error')) ...do something else 
> ...do something 
> > else
> 
> No heaven on earth yet.
> 
> how would I incorporate this kind of functionality into 
> Resultdt<-lapply(PGWide[,240:389], tdt)
> 
> everything would have to be built into the tdt spot in the 
> above statement.
> How does one get the if...else in there? Does one have to do 
> that as one would program a function or could one write the 
> if...else right into "Resultdt<-lapply(PGWide[,240:389], tdt)"
> 
> This works
> > for (few in c(9,10,11,12,243,20)) if
> > (inherits(try(tdt(PGWide[,few])),'try-error')) 
> print("messed up") else
> > print("works")
> [1] "works"
> [1] "works"
> [1] "works"
> [1] "works"
> Error in rep.default(1, nrow(U)) : rep() incorrect type for 
> second argument In addition: Warning messages:
> 1: 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, allow.ambiguous = (parent ==
> 2: 2 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, allow.ambiguous = (parent ==
> 3: 2 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, allow.ambiguous = (parent ==
> 4: 4 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, allow.ambiguous = (parent == [1] "messed up"
> [1] "works"
> Warning message:
> 1 misinheritances in: phase.resolve(g.cs, g.mr, g.fr, 
> as.allele.pair = TRUE, allow.ambiguous = (parent ==
> 
> BUT THIS DOES NOT
> 
> lapply(PGWide[,c(9,10,11,12,,243,20)], if 
> (inherits(try(tdt),'try-error') print("messed up") else 
> print("works"))
> Error: syntax error in "lapply(PGWide[,c(9,10,11,12,,243,20)], if
> (inherits(try(tdt),'try-error') print"
> 
> Any idea why...can it be that one cannot have multiple 
> commands on one line
> > p=7 f=8
> Error: syntax error in "p=7 f"
> 
> in the lapply, how would R know that I was sending the list to tdt?
> 
> 
> --
> Farrel Buchinsky, MD
> Pediatric Otolaryngologist
> Allegheny General Hospital
> Pittsburgh, PA
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list