[R] T.test error help

Steve Lianoglou mailinglist.honeypot at gmail.com
Thu Jul 9 16:06:00 CEST 2009


Hi,

On Jul 9, 2009, at 9:19 AM, Amit Patel wrote:

>
> Hi I am implementing the t.test in a loop and where the data is the  
> same i get an error message.
>
> Error in t.test.default(Samp3, Samp1, na.rm = TRUE, var.equal =  
> FALSE,  :
>  data are essentially constant
>
> The script i am using is
>
> for (i in 1:length(zz[,1])) {
>
> Samp1 <- zz[i,2:17]
> Samp2 <- zz[i,18:33]
> Samp3 <- zz[i,34:47]
> Samp4 <- zz[i,48:63]
>
> TTestResult[i,2] <- t.test(Samp2, Samp1, na.rm=TRUE, var.equal =  
> FALSE, paired=FALSE, conf.level=0.95)$p.value
>
>
> TTestResult[i,3] <- t.test(Samp3, Samp1, na.rm=TRUE, var.equal =  
> FALSE, paired=FALSE, conf.level=0.95)$p.value
>
> TTestResult[i,4] <- t.test(Samp4, Samp1, na.rm=TRUE, var.equal =  
> FALSE, paired=FALSE, conf.level=0.95)$p.value
>
> }
>
> Is there a way to make my loop ignore this problem and go onto the  
> next iteration

Yes, see: ?try and ?tryCatch

For example:

R> log(10)
[1] 2.302585

R> log("a")
Error in log("a") : Non-numeric argument to mathematical function

R> a <- tryCatch(log(10), error=function(e) {
+ cat("There was an error\n")
+ NA
+ })

R> b <- tryCatch(log("a"), error=function(e) {
+ cat("There was an error\n")
+ NA
+ })
There was an error

R> a
[1] 2.302585

R> b
[1] NA

-steve

--
Steve Lianoglou
Graduate Student: Physiology, Biophysics and Systems Biology
Weill Medical College of Cornell University

Contact Info: http://cbio.mskcc.org/~lianos




More information about the R-help mailing list