Transform logicals and numerics to a new class 'binary'. The function try_binary() only coerces to binary if the input is numeric and consists of only zeroes and ones.

as.binary(x)

is.binary(x)

try_binary(x)

Arguments

x

value(s) to convert to binary

Examples

as.binary(c(TRUE, FALSE))
#> <binary>
#> [1] 1 0

as.binary(c(1, 0))
#> <binary>
#> [1] 1 0
as.binary(c(1, 0)) + 3 # not binary anymore
#> [1] 4 3

try_binary(c(0, 1))
#> <binary>
#> [1] 0 1
try_binary(c(2, 3))
#> [1] 2 3
try_binary(c("a", "b"))
#> [1] "a" "b"