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
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 iterationYes, 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
put another condition in your loop if(all.equal(x,y)=TRUE) i=i+1 else t.test... something in that direction. best, daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Amit Patel Gesendet: Thursday, July 09, 2009 9:20 AM An: r-help at r-project.org Betreff: [R] T.test error help 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 ______________________________________________ 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.