isInteger | R Documentation |
isInteger
tests if a given number is an integer.
isInteger(n, tol = .Machine$double.eps)
n |
a vector or an array of values to be tested. |
tol |
a numeric value giving the tolerance level. |
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.
TRUE
if the argument n
has an integer value,
FALSE
otherwise.
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).
Thorn Thaler
is.integer
# 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.