[R] how to judge a virable is a integer?

William Dunlap wdunlap at tibco.com
Sat Oct 18 19:33:05 CEST 2014


3. all.equal(a, as.integer(a))

Note that this one tests if 'a' can be stored accurately as a 32-bit signed
integer.  If you want to know if 'a' can be used as an accurate count, then
you want to test if a+1>a (use abs() in case a is negative).  E.g., try this
for a<-2^49-1, about 5*10^14.

You have to decide what properties of integers you are interested in.

Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Sat, Oct 18, 2014 at 10:02 AM, PO SU <rhelpmaillist at 163.com> wrote:
>
> Tks for your help, after investigate in your link, i find there seems three ways can be adoped:
> 1.    is.wholenumber <- function(x, tol = .Machine$double.eps^0.5)  abs(x - round(x)) < tol)
>  e.g. is.wholenumber(1)
> 2.   x%%1==0
>
>
> 3. all.equal(a, as.integer(a))
>
>
> and also included your last suggestion using floor. and also tks for other helpers!
>
>
>
>
>
>
>
>
> --
>
> PO SU
> mail: desolator88 at 163.com
> Majored in Statistics from SJTU
>
> At 2014-10-18 22:48:15, "Sergio Fonda" <sergio.fonda99 at gmail.com> wrote:
>
>
>
> Sorry for my previous hurry misunderstanding.
> Try this link:
> http://stackoverflow.com/questions/3476782/how-to-check-if-the-number-is-integer
>
>
>
>
> 2014-10-18 16:25 GMT+02:00 PO SU <rhelpmaillist at 163.com>:
>
>
>
> It's due to that, 1 is a numeric, 1.2 is a numeric, though it's true. but deeply, when i want to know 1 is an integer,  there seems no easy way to get the answer.
>
> So, is there anyone happen to know it?
>
>
>
>
>
>
>
>
>
> --
>
>
>
> PO SU
>
> mail: desolator88 at 163.com
>
> Majored in Statistics from SJTU
>
>
>
>
>
>
>
> At 2014-10-18 20:10:09, "S Ellison" <S.Ellison at LGCGroup.com> wrote:
>
>>> But i use a<-10/b ,  b is some value ,may be  5, maybe 5.5
>
>>If you do floating point arithmetic on integers you'll usually get floating point answers, including the 5.0.
>
>>
>
>>See FAQ 7.31 for the usual floating point problem, and ?all.equal for the usual answer to it. You could see if a result is close to an integer by,for example, using all.equal to compare it to itself after rounding.
>
>>
>
>>S
>
>>
>
>>*******************************************************************
>
>>This email and any attachments are confidential. Any use, copying or
>
>>disclosure other than by the intended recipient is unauthorised. If
>
>>you have received this message in error, please notify the sender
>
>>immediately via +44(0)20 8943 7000 or notify postmaster at lgcgroup.com
>
>>and delete this message and any copies from your computer and network.
>
>>LGC Limited. Registered in England 2991879.
>
>>Registered office: Queens Road, Teddington, Middlesex, TW11 0LY, UK
>
> ______________________________________________
>
> 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.
>
>
>
> ______________________________________________
> 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.



More information about the R-help mailing list