Skip to contents

make_binary() converts a variable that can be collapsed to exactly two levels (via make_dicho()) into an integer vector of 0s and 1s. By default, the first level maps to 1L and the second to 0L. Use flip_values = TRUE to reverse the mapping.

When called inside mutate(), metadata is recorded in @metadata@transformations[[col]].

Usage

make_binary(
  x,
  flip_values = FALSE,
  .exclude = NULL,
  .label = NULL,
  .description = NULL
)

Arguments

x

Vector. Same types as make_factor(). Must yield exactly 2 levels (after .exclude) or error.

flip_values

logical(1). If TRUE, map the first level to 0L and the second to 1L. Default maps first level to 1L.

.exclude

character or NULL. Passed directly to make_dicho(). Level names to set to NA before encoding.

.label

character(1) or NULL. Variable label override. Falls back to attr(x, "label") then the column name.

.description

character(1) or NULL. Transformation description.

Value

An integer vector with values 0L, 1L, or NA_integer_.

See also

Examples

library(dplyr)
x <- factor(c("Agree", "Disagree", "Agree", NA),
            levels = c("Agree", "Disagree"))
make_binary(x)
#> [1]  1  0  1 NA
#> attr(,"label")
#> [1] "x"
#> attr(,"labels")
#>    Agree Disagree 
#>        1        0 
#> attr(,"surveytidy_recode")
#> attr(,"surveytidy_recode")$fn
#> [1] "make_binary"
#> 
#> attr(,"surveytidy_recode")$var
#> [1] "x"
#> 
#> attr(,"surveytidy_recode")$description
#> NULL
#>