> test2=list(numeric(0),c(10,20)); > test1=list(c(1),c(1,2,3,4)); > for (i in 1:2){ > tryCatch(wilcox.test(test1[[i]],test2[[i]]),error = function(e) NULL); > } > > I cannot get the p-value of the test for i=2.
You didn't store the results of wilcox.test anywhere. First make it work for data that does not cause errors in wilcox.test: f0 <- function(list1, list2) { stopifnot(length(list1) == length(list2)) sapply(seq_along(list1), function(i) wilcox.test(list1[[i]], list2[[i]])$p.value) } > f0( list(1:4, 5:7), list(11:12, (4:6)+.9)) [1] 0.1333333 0.7000000 Then add the call to tryCatch so it works when there is a problem. I use NA instead of NULL as the output of the error function so it goes into the vector of p.values. Use NULL if you are returning the whole output of wilcox.test instead of just the p.value component. f1 <- function(list1, list2) { stopifnot(length(list1) == length(list2)) sapply(seq_along(list1), function(i)tryCatch( wilcox.test(list1[[i]], list2[[i]])$p.value, error=function(e)NA_real_)) } > f1( list(1:4, 5:7), list(11:12, (4:6)+.9)) [1] 0.1333333 0.7000000 > f1( list(1:4, numeric(0), 5:7), list(11:12, 17, (4:6)+.9)) [1] 0.1333333 NA 0.7000000 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of C Lin > Sent: Tuesday, March 27, 2012 11:36 AM > To: dwinsem...@comcast.net > Cc: r-help@r-project.org > Subject: Re: [R] ignore error getting next result > > > I'm sorry. I do appreciate you are trying to help. However, what I am trying > to do is not > exactly the same as in FAQ. > > If I do the following: > > test2=list(numeric(0),c(10,20)); > test1=list(c(1),c(1,2,3,4)); > for (i in 1:2){ > tryCatch(wilcox.test(test1[[i]],test2[[i]]),error = function(e) NULL); > } > > I cannot get the p-value of the test for i=2. > > any other input? anyone? > > Thanks, > Lin > > > > > CC: r-help@r-project.org > From: dwinsem...@comcast.net > To: bac...@hotmail.com > Subject: Re: [R] ignore error getting next result > Date: Tue, 27 Mar 2012 14:26:40 -0400 > > > > > On Mar 27, 2012, at 2:18 PM, C Lin wrote: > > > As a matter of fact, I did read the FAQ. However, in the FAQ coef() is used > to return the > coefficients of lm() if it succeeded. > I cannot find similar function for pvalue. > > > So your question has nothing to do with the subject line? If you are trying > to get > information about the object returned by the wilcox.test function, then you > should be > looking at the help page in the Value section for that function. > > > -- > David. > > > > > > > > CC: r-help@r-project.org > > From: dwinsem...@comcast.net > > To: bac...@hotmail.com > > Subject: Re: [R] ignore error getting next result > > Date: Tue, 27 Mar 2012 13:40:39 -0400 > > > > > > On Mar 27, 2012, at 12:56 PM, C Lin wrote: > > > > > > > > Dear All, > > > > > > How do I ignore an error and still getting result of next iteration. > > > I am trying to do wilcox.test on a loop, when the test fail, I would > > > like to continue doing the next iteration and getting the p-value. > > > I tried to do tryCatch or try but I cannot retrieve the p-value if > > > the test is not fail. > > > > > > sample code: > > > > > > test2=list(numeric(0),c(10,20)); > > > test1=list(c(1),c(1,2,3,4)); > > > for (i in 1:2){ > > > wtest=wilcox.test(test1[[i]],test2[[i]]) > > > } > > > > > > i=1 will fail, I want to ignore this and get the pvalue for i=2. > > > > Please read the FAQ entry And you would be advise to read through the > > rest of the FAQ as well. > > > > http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-capture-or-ignore-errors- > in-a-long-simulation_003f > > > > > > > > > > Thanks, > > > Lin > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help@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. > > > > David Winsemius, MD > > West Hartford, CT > > > > > > > > David Winsemius, MD > West Hartford, CT > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.