Check if the number is integer
Another alternative is to check the fractional part:
x%%1==0
or, if you want to check within a certain tolerance:
min(abs(c(x%%1, x%%1-1))) < tol
Here's a solution using simpler functions and no hacks:
all.equal(a, as.integer(a))
What's more, you can test a whole vector at once, if you wish. Here's a function:
testInteger <- function(x){
test <- all.equal(x, as.integer(x), check.attributes = FALSE)
if(test == TRUE){ return(TRUE) }
else { return(FALSE) }
}
You can change it to use *apply
in the case of vectors, matrices, etc.