categorize: Categorize a Column

Description Usage See Also Examples

View source: R/forcats.R

Description

Apply 'forcats::fct_collapse' on a character column.

Usage

1
categorize(data, col, ..., other_values = NULL, na_level = "(Missing)")

See Also

Other forcats functions: recode_boolean(), recode_value()

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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")


categorize(data = test_data,
           col = A,
           Odd = as.character(seq(from = 1, 
                                  to   = 10,
                                  by = 2)),
           other_values = "Even",
           na_level = NULL)


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)

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)))


# Recode
recode_value(data = test_data,
             col = A,
             One = as.character(1),
             Two = as.character(2))


recode_boolean(data = test_data,
               col = Group,
               true_value = "Pear",
               false_value = "Apple")

meerapatelmd/rubix documentation built on Sept. 5, 2021, 8:30 p.m.