Create or test for objects of type "logical", and the basic logical
constants.
This function is a wrapper of the function as.logical()
and evaluates
if the object to be coerced can be interpreted as a boolean. Any object :
NA, NA_integer, NA_Date_, (...),
0, 0L, F, FALSE, false, FaLsE, (...),
1, 1L,T, TRUE, true, TrUe, (...),
will be converted as NA, FALSE and TRUE. Any other other will return an
error.
as_any_boolean(x)
Object to be coerced or tested. Can be a vector.
An logical object of the same size.
{
library(dplyr)
as_any_boolean("TRUE")
as_any_boolean(c("1"))
as_any_boolean(0L)
try(as_any_boolean(c('foo')))
as_any_boolean(c(0,1L,0,TRUE,"t","F","FALSE"))
tibble(values = c(0,1L,0,TRUE,"t","F","FALSE")) %>%
mutate(bool_values = as_any_boolean(values))
}
#> Error in as_any_boolean(c("foo")) :
#> x is not in a standard unambiguous format
#> # A tibble: 7 × 2
#> values bool_values
#> <chr> <lgl>
#> 1 0 FALSE
#> 2 1 TRUE
#> 3 0 FALSE
#> 4 TRUE TRUE
#> 5 t TRUE
#> 6 F FALSE
#> 7 FALSE FALSE