Skip to content
Prev 32363 / 63424 Next

'is.integer' (PR#13671)

is.integer() is one of those functions with a name that can be confusing 
-- it looks at the underlying storage type of its argument (e.g., 
integer, floating point, character, etc.) not at the value stored in the 
argument.

So, the type of behavior you see is this:

 > is.integer(1)
[1] FALSE
 > is.integer(as.integer(1))
[1] TRUE
 > is.integer(as.integer(1) * 1.0)
[1] FALSE
 > is.integer(as.integer(NA))
[1] TRUE
 >

Careful reading of ?is.integer does tell you this, but I wouldn't accuse 
that help page of making such information blatantly obvious to new users 
of R.

To test whether a value is an integer value, you can so something like this:

 > is.wholenumber <- function(x, tolerance = .Machine$double.eps^0.5) 
return(abs(x - round(x)) < tolerance)
 > is.wholenumber(1)
[1] TRUE
 > is.wholenumber(seq(1,5,by=0.5))
[1]  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
 >

The 'tolerance' part is to allow for minor deviations that might be due 
to floating point representation issues, e.g., on my computer 1/49 * 49 
does not result in a value that is exactly equal to 1:

 > 1/49 * 49 - 1
[1] -1.110223e-16
 > is.wholenumber(1/49 * 49)
[1] TRUE
 > is.wholenumber(1/49 * 49, tol=0)
[1] FALSE
 >

-- Tony Plate
hzambran.newsgroups at gmail.com wrote: