Type checks - TRUE and FALSE values
predicates-true-false.RdCheck if an object is literally TRUE or FALSE, controlling for NA
values. rlang::is_bool() checks for either TRUE or FALSE.
Usage
are_true(x, na = FALSE)
is_true2(x, na = FALSE)
are_false(x, na = FALSE)
is_false2(x, na = FALSE)
is_bool2(x, na = FALSE)Arguments
- x
[
logical(),any] Forare_*(), a logical vector; foris_*(), an object to test.- na
[
FALSE|NA] What to return forNAvalues.
Value
[
logical(length(x))] Forare_*: the vectorized or result of the test.[
TRUE|FALSE|NA] Foris_*: the scalar result of the test. Ifna != NA, the result is alwaysTRUEorFALSE.
Examples
x <- c(TRUE, FALSE, NA)
# Vectorized tests:
are_true(x) #> c(TRUE, FALSE, FALSE)
#> [1] TRUE FALSE FALSE
are_true(x, na = NA) #> c(TRUE, FALSE, NA)
#> [1] TRUE FALSE NA
are_false(x) #> c(FALSE, TRUE, FALSE)
#> [1] FALSE TRUE FALSE
are_false(x, na = NA) #> c(FALSE, TRUE, NA)
#> [1] FALSE TRUE NA
are_true(logical(0)) #> logical(0)
#> logical(0)
try(are_true(1:3)) #> Error (only works for logical vectors)
#> Error in are_true(1:3) : `x` must be a logical vector.
# Scalar tests:
is_true2(c(TRUE, TRUE)) #> FALSE
#> [1] FALSE
is_true2(TRUE) #> TRUE
#> [1] TRUE
is_true2(NA) #> FALSE
#> [1] FALSE
is_true2(NA, na = NA) #> NA
#> [1] NA
# And similar for is_false2()
is_bool2(TRUE) #> TRUE
#> [1] TRUE
is_bool2(FALSE) #> TRUE
#> [1] TRUE
is_bool2(NA) #> FALSE
#> [1] FALSE
is_bool2(NA, na = NA) #> NA
#> [1] NA