Skip to contents

A function to applied some forcats functions

Usage

fct_clean(
  x,
  na_level = "(Missing)",
  n = NULL,
  other_level = "Other",
  infreq = TRUE
)

Arguments

x

A vector

na_level

If this argument is not null, then `fct_explicit_na` is applied with this argument.

n

If this argument is not null, then `fct_lump_n` is applied with `n` and `other_level`.

other_level

this argument is used if `n` is not null.

infreq

Logical value to apply or not `fct_infreq`.

Examples


x1 <- c(rep(LETTERS[1:9], times = c(10, 40, 5, 27, 1, 1, 1, 1, 1)), rep(NA, 10))
x2 <- factor(x1)

table(x1, useNA = "always")
#> x1
#>    A    B    C    D    E    F    G    H    I <NA> 
#>   10   40    5   27    1    1    1    1    1   10 
table(x2, useNA = "always")
#> x2
#>    A    B    C    D    E    F    G    H    I <NA> 
#>   10   40    5   27    1    1    1    1    1   10 

identical(fct_clean(x1), fct_clean(x2))
#> [1] TRUE

xf <- fct_clean(x1, n = 4)
xf
#>  [1] A         A         A         A         A         A         A        
#>  [8] A         A         A         B         B         B         B        
#> [15] B         B         B         B         B         B         B        
#> [22] B         B         B         B         B         B         B        
#> [29] B         B         B         B         B         B         B        
#> [36] B         B         B         B         B         B         B        
#> [43] B         B         B         B         B         B         B        
#> [50] B         Other     Other     Other     Other     Other     D        
#> [57] D         D         D         D         D         D         D        
#> [64] D         D         D         D         D         D         D        
#> [71] D         D         D         D         D         D         D        
#> [78] D         D         D         D         D         Other     Other    
#> [85] Other     Other     Other     (Missing) (Missing) (Missing) (Missing)
#> [92] (Missing) (Missing) (Missing) (Missing) (Missing) (Missing)
#> Levels: B D A (Missing) Other

table(xf, useNA = "always")
#> xf
#>         B         D         A (Missing)     Other      <NA> 
#>        40        27        10        10        10         0