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]].
Arguments
- x
Vector. Same types as
make_factor(). Must yield exactly 2 levels (after.exclude) or error.- flip_values
logical(1). IfTRUE, map the first level to0Land the second to1L. Default maps first level to1L.- .exclude
characterorNULL. Passed directly tomake_dicho(). Level names to set toNAbefore encoding.- .label
character(1)orNULL. Variable label override. Falls back toattr(x, "label")then the column name.- .description
character(1)orNULL. Transformation description.
See also
Other transformation:
make_dicho(),
make_factor(),
make_flip(),
make_rev()
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
#>
