R – Check if Type of Vector is Character
To check if type of given vector is character in R, call is.character() function and pass the vector as argument to this function. If the given vector is of type character, then is.character() returns TRUE, or else, it returns FALSE.
The syntax to call is.character() to check if type of vector x
is character is
is.character(x)
Return Value
The function returns a logical value.
Examples
In the following program, we take a vector in x
, and check if this vector is a character vector using is.character()
function. We print the value returned by is.character()
.
example.R
x <- c("Hello", "World")
result <- is.character(x)
print(result)
Output
[1] TRUE
Now, let us take a vector x
of some type other than character, and check the value returned by is.character(x)
. Since x
is not of type character, is.character(x)
must return FALSE
.
example.R
x <- c(TRUE, FALSE, FALSE)
result <- is.character(x)
print(result)
Output
[1] FALSE
Conclusion
In this R Tutorial, we learned how to check if given vector is of type character, using is.character() function, with the help of examples.