Tests whether a variable contains only the values 0 and 1.
Details
This function is used internally by spatialRF to determine whether to apply classification-specific methods (e.g., case weighting with case_weights()). The function returns FALSE if:
The variable has more than two unique values
The variable has only one unique value (constant)
The unique values are not exactly 0 and 1 (e.g., 1 and 2, or TRUE and FALSE)
Missing values (NA) are ignored when determining unique values.
Examples
# Binary variable (returns TRUE)
is_binary(
data = data.frame(response = c(0, 0, 0, 1, 1)),
dependent.variable.name = "response"
)
#> [1] TRUE
# Non-binary variable (returns FALSE)
is_binary(
data = data.frame(response = c(1, 2, 3, 4, 5)),
dependent.variable.name = "response"
)
#> [1] FALSE
# Binary but wrong values (returns FALSE)
is_binary(
data = data.frame(response = c(1, 1, 2, 2)),
dependent.variable.name = "response"
)
#> [1] FALSE