Declare a function that applies `forcats::fct_collapse` on a character column.
categorize_ff(...)
library(tidyverse) test_data <- tibble( Group = sample(c("Apple", "Pear", "Maple", "Evergreen"), size = 15, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 15, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 15, replace = TRUE), C = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 15, replace = TRUE), D = sample(c(NA, TRUE, FALSE), size = 15, replace = TRUE) ) %>% dplyr::mutate(E = B) categorize_fruits_and_trees <- categorize_ff(Fruit = c("Apple", "Pear"), Tree = c("Maple", "Evergreen")) categorize_fruits_and_trees(data = test_data, col = Group)#> # A tibble: 15 x 6 #> Group A B C D E #> <chr> <int> <int> <dbl> <lgl> <int> #> 1 Tree 2 NA 6.09 FALSE NA #> 2 Fruit 1 4 6.05 FALSE 4 #> 3 Fruit 2 6 6.04 TRUE 6 #> 4 Fruit 1 6 6.06 NA 6 #> 5 Tree 2 NA 6.02 FALSE NA #> 6 Fruit 1 6 NA NA 6 #> 7 Fruit 2 NA 6.04 FALSE NA #> 8 Tree 1 5 NA NA 5 #> 9 Fruit 3 6 6.05 TRUE 6 #> 10 Tree 1 4 NA NA 4 #> 11 Tree NA NA NA NA NA #> 12 Tree 2 4 6.01 NA 4 #> 13 Tree 1 4 6.04 NA 4 #> 14 Tree 1 4 6.06 FALSE 4 #> 15 Tree 2 5 NA NA 5test_data2 <- tibble( Group2 = sample(c("Apple", "Pear", "Maple", "Evergreen"), size = 15, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 15, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 15, replace = TRUE), C = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 15, replace = TRUE), D = sample(c(NA, TRUE, FALSE), size = 15, replace = TRUE) ) %>% dplyr::mutate(E = B) test_data3 <- tibble( Group3 = sample(c("Apple", "Pear", "Maple", "Evergreen"), size = 15, replace = TRUE), A = sample(c(NA_integer_, 1:3), size = 15, replace = TRUE), B = sample(c(NA_integer_, 4:6), size = 15, replace = TRUE), C = sample(c(NA_real_, seq(from = 6.01, to = 6.09, by = 0.01)), size = 15, replace = TRUE), D = sample(c(NA, TRUE, FALSE), size = 15, replace = TRUE) ) %>% dplyr::mutate(E = B) categorize_fruits_and_trees(data = test_data2, col = Group2)#> # A tibble: 15 x 6 #> Group2 A B C D E #> <chr> <int> <int> <dbl> <lgl> <int> #> 1 Fruit 3 4 6.03 FALSE 4 #> 2 Tree NA 6 6.05 FALSE 6 #> 3 Tree 3 4 6.01 NA 4 #> 4 Tree 2 NA 6.08 TRUE NA #> 5 Tree NA 6 6.04 FALSE 6 #> 6 Fruit 3 6 6.02 NA 6 #> 7 Fruit NA 5 6.01 NA 5 #> 8 Fruit 2 4 6.04 TRUE 4 #> 9 Fruit 2 6 6.09 NA 6 #> 10 Tree NA 4 6.03 FALSE 4 #> 11 Tree 1 NA 6.02 NA NA #> 12 Tree 2 4 6.07 FALSE 4 #> 13 Tree 3 NA 6.03 FALSE NA #> 14 Fruit 2 4 6.08 NA 4 #> 15 Fruit 2 5 NA NA 5categorize_fruits_and_trees(data = test_data3, col = Group3)#> # A tibble: 15 x 6 #> Group3 A B C D E #> <chr> <int> <int> <dbl> <lgl> <int> #> 1 Tree 3 NA NA TRUE NA #> 2 Tree 3 4 6.08 TRUE 4 #> 3 Tree 3 4 6.08 TRUE 4 #> 4 Fruit 2 NA 6.09 NA NA #> 5 Fruit 3 NA 6.03 NA NA #> 6 Tree 3 5 6.05 TRUE 5 #> 7 Tree 3 NA 6.01 FALSE NA #> 8 Tree 3 6 6.09 NA 6 #> 9 Tree 1 5 6.01 TRUE 5 #> 10 Tree 2 6 6.09 TRUE 6 #> 11 Fruit 1 5 6.09 TRUE 5 #> 12 Tree 1 5 6.01 FALSE 5 #> 13 Tree 3 6 6.04 TRUE 6 #> 14 Tree 3 6 6.02 FALSE 6 #> 15 Tree NA 5 6.03 FALSE 5