is_character_vector {chinese.misc} | R Documentation |
A Convenient Version of is.character
Description
This function checks to see if the object is a character vector. It is designed to have different
actions from is.character
and thus sometimes more convenient. See Details.
Usage
is_character_vector(x, len = NULL, allow_all_na = TRUE)
Arguments
x |
object to be checked |
len |
numeric vector represents the permitted length of character vector. If an
object is a character vector, but its length is not in |
allow_all_na |
for length>1 character vector whose elements are
all |
Details
Sometimes we want to check if an object is a character vector. But is.character
cannot
do this, because it also returns TRUE
for a character matrix or data frame.
What's more, we usually not only want to see if an object is of class
character, but also want to see
if it is valid, that is, can be passed to other functions without errors. But is.character
even returns TRUE
for character(0)
.
Also, is.character(NA)
returns FALSE
, but
is.character(as.character(NA))
returns TRUE
, but in fact there is really
no difference between the two for users and many functions that do not allow NA
.
We list below the returns of is.character2
:
(1) if the object is
NULL
,is.character2
returnsFALSE
.(2) if the object is of length 0, it always returns
FALSE
.(3) if the object is not vector,
FALSE
.(4) if it has only one element and this element is
NA
, under all circumstances it returnsFALSE
.(5) if the vector is of length>1, all the elements are
NA
, but the vector's class is not character, it returnsFALSE
.(6) if a character vector is of length>1, and all the elements are
NA
, then the result depends on argumentallow_all_na
, ifallow_all_na = TRUE
, thenTRUE
, otherwise,FALSE
.
Value
TRUE
or FALSE
.
Examples
is_character_vector(character(0))
is_character_vector(NA)
is_character_vector(as.character(NA))
is_character_vector(c(NA, NA))
is_character_vector(as.character(c(NA,NA)))
is_character_vector(as.character(c(NA, NA)), allow_all_na = FALSE)
is_character_vector(as.character(c(NA, NA)), allow_all_na = TRUE)
is_character_vector(matrix(c("a", "b", "c", "d"), nr = 2))
is_character_vector(c("a", "b", "c"), len = c(1, 10))
is_character_vector(c("a", "b", "c"), len = c(1:10))