Convert binary data to factors based on column name
binary2factor(.data, ...)
(data.frame) A data frame or data frame extension (e.g. a tibble)
Name-value pairs. The names of columns to be transformed
(data.frame) An object of the same type as .data
df <- data.frame(
a = c(1, 0, NA, 1, 0),
b = c("y", "n", NA, "Y", "n"),
c = c("yes", "no", NA, "Yes", "No"),
d = c(TRUE, FALSE, NA, TRUE, FALSE),
e = c(1, 2, 3, 4, 5)
)
binary2factor(df, a, b:d)
#> a b c d e
#> 1 a b c d 1
#> 2 Not_a Not_b Not_c Not_d 2
#> 3 <NA> <NA> <NA> <NA> 3
#> 4 a b c d 4
#> 5 Not_a Not_b Not_c Not_d 5
df %>%
binary2factor(-e)
#> a b c d e
#> 1 a b c d 1
#> 2 Not_a Not_b Not_c Not_d 2
#> 3 <NA> <NA> <NA> <NA> 3
#> 4 a b c d 4
#> 5 Not_a Not_b Not_c Not_d 5