Uses dplyr::n_distinct() to check if there are only two unique values.

is_binary(x, na.rm = TRUE)

Arguments

x

A vector.

na.rm

logical; Should NA be ignored, TRUE by default.

Value

TRUE if only 2 unique values.

Examples

if (is_binary(x <- c("Yes", "No"))) x == "Yes"
#> [1]  TRUE FALSE