Learn R Programming

ttutils (version 1.0-1.1)

isInteger: Test For Integrity

Description

isInteger tests if a given number is an integer.

Usage

isInteger(n, tol = .Machine$double.eps)

Arguments

n

a vector or an array of values to be tested.

tol

a numeric value giving the tolerance level.

Value

TRUE if the argument n has an integer value, FALSE otherwise.

Details

As opposed to is.integer this function tests for integrity of a given value, rather than being of type integer.

In R integers are specified by the suffix L (e.g. 1L), whereas all other numbers are of class numeric independent of their value. The function is.integer does not test whether a given variable has an integer value, but whether it belongs to the class integer.

In contrast, the function isInteger compares the difference between its argument and its rounded argument. If it is smaller than some predefined tolerance level, the variable is regarded as integer.

See Also

is.integer

Examples

Run this code
# NOT RUN {
# isInteger tests if the _value_ of a variable is an integer
# 'c' as opposed to 'list' coerces its arguments!
isInteger(c("test", 1, 2, 2.1))     # FALSE FALSE FALSE FALSE
isInteger(list("test", 1, 2, 2.1))  # FALSE TRUE TRUE FALSE

class(1L)  # integer
typeof(1L) # integer
class(1)   # numeric
typeof(1)  # double

# is.integer tests if the _class_ of a variable is 'integer'
is.integer(c("test", 1, 2))    # FALSE
is.integer(list("test", 1, 2)) # FALSE
is.integer(1)                  # FALSE
is.integer(1L)                 # TRUE 
# }

Run the code above in your browser using DataLab