[R] The elegant way to test if a number is a whole number
gunter.berton at gene.com
Thu Sep 8 20:37:48 CEST 2011
Have a close look at ?storage.mode and ?typeof.
The problem here, as usual and as you seem to allude is: define
"integer" . Mathematically, there is an unequivocal definition, but
not so in finite precision arithmetic. Are you referring to the
machine representation (typeof()), or the approximate value. And if
the latter, how approximate?
I think both your solution and its perceived "beauty" will depend on
your exact definition and context.
You may receive wiser advice. If so, please ignore mine.
On Thu, Sep 8, 2011 at 11:27 AM, Alexander Engelhardt
<alex at chaotic-neutral.de> wrote:
> x <- 0.2*5
> gives me FALSE because R stores it as a float number, right?
> Is there an elegant way to work around that problem? Right now
> I'm using
> x <- 0.2*5
> round(x) == x
> which returns TRUE. But more strictly I should use all.equal(),
> I somehow just don't like the--pardon--ugliness of those pieces
> of code. Maybe there is a beautiful way to write that. If not,
> no big problem -- I just like beautiful code :-)
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
"Men by nature long to get on to the ultimate truths, and will often
be impatient with elementary studies or fight shy of them. If it were
possible to reach the ultimate truths without the elementary studies
usually prefixed to them, these would not be preparatory studies but
-- Maimonides (1135-1204)
Genentech Nonclinical Biostatistics
More information about the R-help