7 Predicate functions


Predicate functions are functions that return a single TRUE or FALSE. You use predicate functions to check if your input meets some condition. For example, is.character() is a predicate function that returns TRUE if its input is of type character and FALSE otherwise.

#> [1] TRUE
#> [1] FALSE

R does not have scalars. "a" is actually a character vector of length 1. is.character() and similar functions return a single value for all vectors, whether they have 1 element or many.

x <- c("a", "b")
#> [1] TRUE

In atomic vectors like x, all the elements must be the same type. The type of the individual elements will always be the same as the type of the atomic vector. You can use the function typeof() to find the type of a vector (or any other object in R).

#> [1] "character"

is.character() simply checks whether x is of type character.

If the vector is not a character vector, is.character() will return FALSE.

y <- c(1, 3)
#> [1] "double"
#> [1] FALSE

Lists can have elements of any type. Even if all the elements of a list are of type character, the list is not a character vector, and is.character() will return FALSE.

z <- list("a", "b")
#> [1] "list"
#> [1] FALSE