is_positive_integer {chinese.misc} | R Documentation |
A Convenient Version of is.integer
Description
This function checks if all elements of an object can be taken to be valid integers.
Usage
is_positive_integer(x, len = NULL)
Arguments
x |
an object to be checked |
len |
numeric vector specifying the allowed length of the |
Details
The reasons to use is_positive_integer
are:
(1) We often check if an object is a vector of positive integer. But
is.numeric
cannot do this because it also returnsTRUE
for a numeric matrix.(2) Sometimes
is.integer
returns a too strict result. For example,is.integer(3.0)
returnsFALSE
, but the number 3.0 is valid in codes such asrep(10, 3.0)
, that is to say, as long as a number can be taken to be a valid integer, we take it to be a integer, even whenis.integer
returnsFALSE
.(3)
is_positive_integer
returnsFALSE
for length = 0 object, even when it isinteger(0)
. To let the function return this result is because integer of length 0 is a invalid input for many functions.(4)
is_positive_integer
returnsFALSE
for any object that containsNA
, so that object that gets aTRUE
from this function is more likely to be a valid value to be passed to other functions.
Value
TRUE
or FALSE
Examples
is_positive_integer(NULL)
is_positive_integer(as.integer(NA))
is_positive_integer(integer(0))
is_positive_integer(3.0)
is_positive_integer(3.3)
is_positive_integer(1:5)
is_positive_integer(1:5, len = c(2, 10))
is_positive_integer(1:5, len = c(2:10))