isInteger: Test For Integrity

View source: R/utils.R

isIntegerR Documentation

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.

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.

Value

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

Note

The R function c concatenates its argument and forms a vector. In doing so, it coerces the values to a common type. Hence, attention has to be paid, because isInteger may give some unexpected results in this case. The R command list, however, does not coerce its arguments (see the example).

Author(s)

Thorn Thaler

See Also

is.integer

Examples

# 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 

ttutils documentation built on April 5, 2022, 1:15 a.m.

Related to isInteger in ttutils...