I'm trying to do realize the following: I have 4 condtions. If all conditions are satisfied I will paste("PASS") If any of these is not satisfied I will paste("FAIL"). But I have to paste the corresponding failure. ifelse is a good solution but for a 2 conditions. Maybe switch or something like this. Does anyone have an idea how to do? Thanks in advance. Adel -- View this message in context: http://www.nabble.com/if-one-of-4-conditions-is-not-satisfied-tp18214194p18214194.html Sent from the R help mailing list archive at Nabble.com.
on 07/01/2008 07:40 AM mysimbaa wrote:> I'm trying to do realize the following: > I have 4 condtions. > If all conditions are satisfied I will paste("PASS") > If any of these is not satisfied I will paste("FAIL"). But I have to paste > the corresponding failure. > > ifelse is a good solution but for a 2 conditions. Maybe switch or something > like this. > > Does anyone have an idea how to do? > > Thanks in advance. > AdelAn easy way would be to have your condition results, which are presumably TRUE/FALSE, in a vector such as: Cond <- c(TRUE, FALSE, FALSE, TRUE) Then you could use all() to check to see if they are 'all' TRUE: > all(Cond) [1] FALSE Then use which() to get the index of the FALSE elements: > which(!Cond) [1] 2 3 If you have your test labels in a character vector, such as: Cond.Vec <- c("Cond 1", "Cond 2", "Cond 3", "Cond 4") You could then use: if (all(Cond)) { Out <- "PASS" } else { Out <- paste("FAIL:", paste(Cond.Vec[which(!Cond)], collapse = " & ")) } > Out [1] "FAIL: Cond 2 & Cond 3" See ?all and ?which HTH, Marc Schwartz
use c() to make your condition a vector, try to use all() to return a logical value for the entire condition vector . On 2008-7-1, at ??8:40, mysimbaa wrote:> > I'm trying to do realize the following: > I have 4 condtions. > If all conditions are satisfied I will paste("PASS") > If any of these is not satisfied I will paste("FAIL"). But I have to > paste > the corresponding failure. > > ifelse is a good solution but for a 2 conditions. Maybe switch or > something > like this. > > Does anyone have an idea how to do? > > Thanks in advance. > Adel > -- > View this message in context: http://www.nabble.com/if-one-of-4-conditions-is-not-satisfied-tp18214194p18214194.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.----------------------------------------------- Peng Jiang ?? ,Ph.D. Candidate Antai College of Economics & Management ???????? Department of Mathematics ??? Shanghai Jiaotong University (Minhang Campus) 800 Dongchuan Road 200240 Shanghai P. R. China