This function automatically detects and converts binary variables (with exactly two unique non-NA values) in a data frame to factors. Currently only supports conversion from binary variables.

convert2factor(data, from = "binary")

Arguments

data

A data frame containing variables to be converted.

from

Type of variables to convert. Currently only supports "binary" (default).

Value

The original data frame with binary variables converted to factors. The function preserves existing factor variables and skips non-binary columns.

Examples

df <- data.frame(a = c(0,1,0), b = c("Y","N","Y"))
converted_df <- convert2factor(df)
#>  a has been converted to a factor: 0, 1
#>  b has been converted to a factor: N, Y