Apply `forcats::fct_recode` to a character column to convert to logical.

recode_boolean(data, col, true_value, false_value)

See also

Other forcats functions: categorize(), recode_value()

Examples

library(tidyverse) test_data <- tibble( Group = sample(c("Apple", "Pear"), size = 10, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 10, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 10, replace = TRUE), C = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 10, replace = TRUE), D = sample(c(NA, TRUE, FALSE), size = 10, replace = TRUE) ) %>% dplyr::mutate(E = B) categorize(data = test_data, col = A, Odd = as.character(seq(from = 1, to = 10, by = 2)), other_values = "Even")
#> Warning: Problem with `mutate()` input `A`. #> Unknown levels in `f`: 5, 7, 9 #> Input `A` is `(function (.f, ..., other_level = NULL, group_other = "DEPRECATED") ...`.
#> Warning: Unknown levels in `f`: 5, 7, 9
#> # A tibble: 10 x 6 #> Group A B C D E #> <chr> <chr> <int> <dbl> <lgl> <int> #> 1 Apple Odd 5 6.06 FALSE 5 #> 2 Apple NA 4 6.09 NA 4 #> 3 Pear Odd NA 6.06 TRUE NA #> 4 Pear NA 6 6.09 NA 6 #> 5 Pear Even NA 6.09 TRUE NA #> 6 Apple Odd NA 6.01 FALSE NA #> 7 Apple NA 4 6.03 NA 4 #> 8 Pear NA 5 6.09 NA 5 #> 9 Pear NA NA 6.01 TRUE NA #> 10 Pear Odd 4 6.07 NA 4
categorize(data = test_data, col = A, Odd = as.character(seq(from = 1, to = 10, by = 2)), other_values = "Even", na_level = NULL)
#> Warning: Problem with `mutate()` input `A`. #> Unknown levels in `f`: 5, 7, 9 #> Input `A` is `(function (.f, ..., other_level = NULL, group_other = "DEPRECATED") ...`.
#> Warning: Unknown levels in `f`: 5, 7, 9
#> # A tibble: 10 x 6 #> Group A B C D E #> <chr> <chr> <int> <dbl> <lgl> <int> #> 1 Apple Odd 5 6.06 FALSE 5 #> 2 Apple NA 4 6.09 NA 4 #> 3 Pear Odd NA 6.06 TRUE NA #> 4 Pear NA 6 6.09 NA 6 #> 5 Pear Even NA 6.09 TRUE NA #> 6 Apple Odd NA 6.01 FALSE NA #> 7 Apple NA 4 6.03 NA 4 #> 8 Pear NA 5 6.09 NA 5 #> 9 Pear NA NA 6.01 TRUE NA #> 10 Pear Odd 4 6.07 NA 4
categorize(data = test_data, col = A, Odd = as.character(seq(from = 1, to = 10, by = 2)), Even = as.character(seq(from = 2, to = 10, by = 2)), na_level = NULL)
#> Warning: Problem with `mutate()` input `A`. #> Unknown levels in `f`: 5, 7, 9, 4, 6, 8, 10 #> Input `A` is `(function (.f, ..., other_level = NULL, group_other = "DEPRECATED") ...`.
#> Warning: Unknown levels in `f`: 5, 7, 9, 4, 6, 8, 10
#> # A tibble: 10 x 6 #> Group A B C D E #> <chr> <chr> <int> <dbl> <lgl> <int> #> 1 Apple Odd 5 6.06 FALSE 5 #> 2 Apple NA 4 6.09 NA 4 #> 3 Pear Odd NA 6.06 TRUE NA #> 4 Pear NA 6 6.09 NA 6 #> 5 Pear Even NA 6.09 TRUE NA #> 6 Apple Odd NA 6.01 FALSE NA #> 7 Apple NA 4 6.03 NA 4 #> 8 Pear NA 5 6.09 NA 5 #> 9 Pear NA NA 6.01 TRUE NA #> 10 Pear Odd 4 6.07 NA 4
categorize(data = test_data, col = A, Odd = as.character(seq(from = 1, to = 10, by = 2)), Even = as.character(seq(from = 2, to = 10, by = 2)))
#> Warning: Problem with `mutate()` input `A`. #> Unknown levels in `f`: 5, 7, 9, 4, 6, 8, 10 #> Input `A` is `(function (.f, ..., other_level = NULL, group_other = "DEPRECATED") ...`.
#> Warning: Unknown levels in `f`: 5, 7, 9, 4, 6, 8, 10
#> # A tibble: 10 x 6 #> Group A B C D E #> <chr> <chr> <int> <dbl> <lgl> <int> #> 1 Apple Odd 5 6.06 FALSE 5 #> 2 Apple NA 4 6.09 NA 4 #> 3 Pear Odd NA 6.06 TRUE NA #> 4 Pear NA 6 6.09 NA 6 #> 5 Pear Even NA 6.09 TRUE NA #> 6 Apple Odd NA 6.01 FALSE NA #> 7 Apple NA 4 6.03 NA 4 #> 8 Pear NA 5 6.09 NA 5 #> 9 Pear NA NA 6.01 TRUE NA #> 10 Pear Odd 4 6.07 NA 4
# Recode recode_value(data = test_data, col = A, One = as.character(1), Two = as.character(2))
#> # A tibble: 10 x 6 #> Group A B C D E #> <chr> <chr> <int> <dbl> <lgl> <int> #> 1 Apple 3 5 6.06 FALSE 5 #> 2 Apple NA 4 6.09 NA 4 #> 3 Pear 3 NA 6.06 TRUE NA #> 4 Pear NA 6 6.09 NA 6 #> 5 Pear Two NA 6.09 TRUE NA #> 6 Apple 3 NA 6.01 FALSE NA #> 7 Apple NA 4 6.03 NA 4 #> 8 Pear NA 5 6.09 NA 5 #> 9 Pear NA NA 6.01 TRUE NA #> 10 Pear One 4 6.07 NA 4
recode_boolean(data = test_data, col = Group, true_value = "Pear", false_value = "Apple")
#> # A tibble: 10 x 6 #> Group A B C D E #> <lgl> <int> <int> <dbl> <lgl> <int> #> 1 FALSE 3 5 6.06 FALSE 5 #> 2 FALSE NA 4 6.09 NA 4 #> 3 TRUE 3 NA 6.06 TRUE NA #> 4 TRUE NA 6 6.09 NA 6 #> 5 TRUE 2 NA 6.09 TRUE NA #> 6 FALSE 3 NA 6.01 FALSE NA #> 7 FALSE NA 4 6.03 NA 4 #> 8 TRUE NA 5 6.09 NA 5 #> 9 TRUE NA NA 6.01 TRUE NA #> 10 TRUE 1 4 6.07 NA 4