Add additional levels to a factor
fct_expand(f, ..., after = Inf)
fct_drop()
to drop unused factor levels.
f <- factor(sample(letters[1:3], 20, replace = TRUE))
f
#> [1] c a a c b b a b c b b a c a b c b c c c
#> Levels: a b c
fct_expand(f, "d", "e", "f")
#> [1] c a a c b b a b c b b a c a b c b c c c
#> Levels: a b c d e f
fct_expand(f, letters[1:6])
#> [1] c a a c b b a b c b b a c a b c b c c c
#> Levels: a b c d e f
fct_expand(f, "Z", after = 0)
#> [1] c a a c b b a b c b b a c a b c b c c c
#> Levels: Z a b c