Apply `forcats::fct_recode` on a character column.

recode_value(data, col, ...)

See also

Other forcats functions: categorize(), recode_boolean()

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 Even 6 6.01 NA 6 #> 2 Pear Odd 6 6.04 NA 6 #> 3 Pear Odd 5 6.01 FALSE 5 #> 4 Pear Even 4 6.07 TRUE 4 #> 5 Pear Even NA 6.04 TRUE NA #> 6 Pear NA 6 6.03 NA 6 #> 7 Apple Even 5 6.02 FALSE 5 #> 8 Apple Even NA 6.03 FALSE NA #> 9 Pear Odd 5 NA FALSE 5 #> 10 Pear Odd 4 6.06 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 Even 6 6.01 NA 6 #> 2 Pear Odd 6 6.04 NA 6 #> 3 Pear Odd 5 6.01 FALSE 5 #> 4 Pear Even 4 6.07 TRUE 4 #> 5 Pear Even NA 6.04 TRUE NA #> 6 Pear NA 6 6.03 NA 6 #> 7 Apple Even 5 6.02 FALSE 5 #> 8 Apple Even NA 6.03 FALSE NA #> 9 Pear Odd 5 NA FALSE 5 #> 10 Pear Odd 4 6.06 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 Even 6 6.01 NA 6 #> 2 Pear Odd 6 6.04 NA 6 #> 3 Pear Odd 5 6.01 FALSE 5 #> 4 Pear Even 4 6.07 TRUE 4 #> 5 Pear Even NA 6.04 TRUE NA #> 6 Pear NA 6 6.03 NA 6 #> 7 Apple Even 5 6.02 FALSE 5 #> 8 Apple Even NA 6.03 FALSE NA #> 9 Pear Odd 5 NA FALSE 5 #> 10 Pear Odd 4 6.06 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 Even 6 6.01 NA 6 #> 2 Pear Odd 6 6.04 NA 6 #> 3 Pear Odd 5 6.01 FALSE 5 #> 4 Pear Even 4 6.07 TRUE 4 #> 5 Pear Even NA 6.04 TRUE NA #> 6 Pear NA 6 6.03 NA 6 #> 7 Apple Even 5 6.02 FALSE 5 #> 8 Apple Even NA 6.03 FALSE NA #> 9 Pear Odd 5 NA FALSE 5 #> 10 Pear Odd 4 6.06 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 Two 6 6.01 NA 6 #> 2 Pear One 6 6.04 NA 6 #> 3 Pear One 5 6.01 FALSE 5 #> 4 Pear Two 4 6.07 TRUE 4 #> 5 Pear Two NA 6.04 TRUE NA #> 6 Pear NA 6 6.03 NA 6 #> 7 Apple Two 5 6.02 FALSE 5 #> 8 Apple Two NA 6.03 FALSE NA #> 9 Pear 3 5 NA FALSE 5 #> 10 Pear One 4 6.06 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 2 6 6.01 NA 6 #> 2 TRUE 1 6 6.04 NA 6 #> 3 TRUE 1 5 6.01 FALSE 5 #> 4 TRUE 2 4 6.07 TRUE 4 #> 5 TRUE 2 NA 6.04 TRUE NA #> 6 TRUE NA 6 6.03 NA 6 #> 7 FALSE 2 5 6.02 FALSE 5 #> 8 FALSE 2 NA 6.03 FALSE NA #> 9 TRUE 3 5 NA FALSE 5 #> 10 TRUE 1 4 6.06 NA 4