Hi listers, If I would like to check if a variable contains certain value, I would write: if (10 %in% x) And If I would like to check the opposite, that 10 is not into x. How would be? Thanks in advance, Marcio -- View this message in context: http://r.789695.n4.nabble.com/Condition-in-tp2538110p2538110.html Sent from the R help mailing list archive at Nabble.com.
!10 %in% x (or !(10 %in% x) if you don't believe in R's precedence rules. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 13 Sep 2010, Mestat wrote:> > Hi listers, > If I would like to check if a variable contains certain value, I would > write: > if (10 %in% x) > And If I would like to check the opposite, that 10 is not into x. How would > be? > Thanks in advance, > Marcio > -- > View this message in context: http://r.789695.n4.nabble.com/Condition-in-tp2538110p2538110.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. >
On Sep 13, 2010, at 5:43 PM, Mestat wrote:> > Hi listers, > If I would like to check if a variable contains certain value, I would > write: > if (10 %in% x) > And If I would like to check the opposite, that 10 is not into x. > How would > be?Not sure about your terminology, "into x"?. why wouldn't it be if (x %in% 10) ... oh, I see, it's that single argument to if() thing again. > x <- 10 > y <- c(10,9) > if (y %in% x) TRUE [1] TRUE Warning message: In if (y %in% x) TRUE : the condition has length > 1 and only the first element will be used ?all ?any > if (all(y %in% x)) TRUE else FALSE [1] FALSE > if (all(x %in% y)) TRUE else FALSE [1] TRUE > if (any(x %in% y)) TRUE else FALSE [1] TRUE > if (any(x %in% y)) TRUE else FALSE [1] TRUE So decide what you really want to test. If you want to check to see if two groups are the same from a set theoretic viewpoint then setdiff() could be used. David Winsemius, MD West Hartford, CT