Sometimes numeric values can appear to be whole numbers but are actually represented in the computer as floating-point values. In these cases, simple inspection of a value will not tell you if it is a whole number or not. This function tests if a number is "close enough" to an integer to be a whole number. Note that is.integer
will indicate if a value is of class integer (which if it is, will always be a whole number), but objects of class numeric
will not evaluate to TRUE
even if they are "supposed" to represent integers.
Examples
x <- c(4, 12 / 3, 21, 21.1)
is.wholeNumber(x)
#> [1] TRUE TRUE TRUE FALSE