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

S Ellison S.Ellison at LGCGroup.com
Mon Oct 20 15:08:57 CEST 2014


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

Closer, but be aware that all.equal will not always return TRUE or FALSE and - more importantly - as.integer truncates towards zero and does NOT generally round to the nearest integer.

a <- 4 - sqrt(2)^2 #Analytically 2
all.equal(a, as.integer(a))
# [1] "Mean relative difference: 0.5"
#because
as.integer(a)
# [1] 1

To return FALSE from all.equal, wrap it in something like

if(all.equal(a, round(a))==TRUE) TRUE else FALSE


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


More information about the R-help mailing list