-----Original Message-----
From: Albyn Jones [mailto:jones at reed.edu]
Sent: Sunday, December 03, 2006 9:42 PM
To: John Fox
Cc: bob at statland.org; 'Yeasmin Khandakar'; 'R-sig-teaching'
Subject: Re: [R-sig-teaching] checking even/odd in R
One solution for the exact equality problem is to start by
rounding or truncating to an integer, depending of course on
what the origin of the dataset was, and the point of the
operation. The sign doesn't matter;
[1] 1
floor(x) rounds down to an integer
ceiling(x) rounds up
round(x) rounds to the nearest integer, rounding to an even integer
in case of ties
albyn
Quoting John Fox <jfox at mcmaster.ca>:
Dear Robert and Yeasmin,
You could test that the number mod 2 is 0; e.g.,
[1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE
A potential problem is that testing exact equality is
probably better off checking that abs(x) %% 2 is less than
small number such as sqrt(.Machine$double.eps).
I hope this helps,
John
--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox
--------------------------------
-----Original Message-----
From: r-sig-teaching-bounces at r-project.org
[mailto:r-sig-teaching-bounces at r-project.org] On Behalf Of
Hayden
Sent: Sunday, December 03, 2006 8:24 PM
To: Yeasmin Khandakar
Cc: R-sig-teaching
Subject: Re: [R-sig-teaching] checking even/odd in R
I do not know of anything specific to r but standard programming
tricks may do it. For example, if the number is x, compute y=x/2,
let z be y rounded to the nearest whole number, and check
it does, x is even. There may be rounding issues; I doubt
data as integers. You might have to do this in an
sees the numbers as integers -- or text. AWK comes to mind.
You may also want code to check for x values like 23.456.
Dear all,
I'm in search for a function which can be used to check
numeriacl value (say, x) is even/odd.
Any help is highly appreciated.
Thanks
Yeasmin
--Boundary_(ID_5eoWS9sJoAbqcSKM1OUsmg)
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline