is_binary: Check if variable is binary with values 0 and 1

View source: R/is_binary.R

is_binaryR Documentation

Check if variable is binary with values 0 and 1

Description

Tests whether a variable contains only the values 0 and 1.

Usage

is_binary(data = NULL, dependent.variable.name = NULL)

Arguments

data

Data frame containing the variable to check.

dependent.variable.name

Character string with the name of the variable to test. Must be a column name in data.

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.

Value

Logical. TRUE if the variable contains exactly two unique values (0 and 1), FALSE otherwise.

See Also

case_weights()

Other preprocessing: auto_cor(), auto_vif(), case_weights(), default_distance_thresholds(), double_center_distance_matrix(), make_spatial_fold(), make_spatial_folds(), the_feature_engineer(), weights_from_distance_matrix()

Examples

# Binary variable (returns TRUE)
is_binary(
  data = data.frame(response = c(0, 0, 0, 1, 1)),
  dependent.variable.name = "response"
)

# Non-binary variable (returns FALSE)
is_binary(
  data = data.frame(response = c(1, 2, 3, 4, 5)),
  dependent.variable.name = "response"
)

# Binary but wrong values (returns FALSE)
is_binary(
  data = data.frame(response = c(1, 1, 2, 2)),
  dependent.variable.name = "response"
)


spatialRF documentation built on Dec. 20, 2025, 1:07 a.m.