Hi, I run into problem when writing a syntax, I don't know syntax that will return true or false if an integer is odd or even. Thanks OYEYEMI, Gafar Matanmi Department of Statistics University of Ilorin P.M.B 1515 Ilorin, Kwara State Nigeria Tel: +2348052278655 Tel: +2348068241885 [[alternative HTML version deleted]]
On Thu, 1 Jul 2010, Yemi Oyeyemi wrote:> Hi, I run into problem when writing a syntax, I don't know syntax that > will return true or false if an integer is odd or even. ThanksSee ?"%%" example("%%") ?"==" HTH, Chuck> > OYEYEMI, Gafar Matanmi[snip] Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
On Jul 1, 2010, at 10:40 AM, Yemi Oyeyemi wrote:> Hi, I run into problem when writing a syntax, I don't know syntax that will return true or false if an integer is odd or even. > Thanksx <- 1:10> x[1] 1 2 3 4 5 6 7 8 9 10 # modulo division> x %% 2 == 0[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE is.even <- function(x) x %% 2 == 0> is.even(x)[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE> is.odd <- function(x) x %% 2 != 0> is.odd(x)[1] TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE Be aware that this will work for integer values. If you have floating point values that are 'close to' integer values, the exact comparison to 0 will be problematic. This may occur as the result of calculations where the result is 'close to' an integer value. In which case, you may want to review: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f HTH, Marc Schwartz
On Jul 1, 2010, at 11:40 AM, Yemi Oyeyemi wrote:> Hi, I run into problem when writing a syntax, I don't know syntax > that will return true or false if an integer is odd or even. > Thanks >> 1:12 %% 2 == 0 [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE> OYEYEMI, Gafar Matanmi > > Department of Statistics > > University of Ilorin > > P.M.B 1515 > > Ilorin, Kwara State > > Nigeria > > Tel: +2348052278655 > > Tel: +2348068241885 > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
On Thu, 2010-07-01 at 08:40 -0700, Yemi Oyeyemi wrote:> Hi, I run into problem when writing a syntax, I don't know syntax that will return true or false if an integer is odd or even. > Thanks > > OYEYEMI, Gafar Matanmi > > Department of Statistics > > University of IlorinHi Yemi! Your problem is solve wiht "!" and "%%", Look this example> x <- 1:11 > x[1] 1 2 3 4 5 6 7 8 9 10 11> x%%2[1] 1 0 1 0 1 0 1 0 1 0 1> !(x%%2)[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
Hi r-help-bounces at r-project.org napsal dne 01.07.2010 17:40:22:> Hi, I run into problem when writing a syntax, I don't know syntax thatwill> return true or false if an integer is odd or even.fff <- function(x) as.logical(x%%2) Regards Petr> Thanks > > OYEYEMI, Gafar Matanmi > > Department of Statistics > > University of Ilorin > > P.M.B 1515 > > Ilorin, Kwara State > > Nigeria > > Tel: +2348052278655 > > Tel: +2348068241885 > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.