Skip to content

Add additional levels to a factor

Usage

fct_expand(f, ..., after = Inf)

Arguments

f

A factor (or character vector).

...

Additional levels to add to the factor. Levels that already exist will be silently ignored.

after

Where should the new values be placed?

See also

fct_drop() to drop unused factor levels.

Examples

f <- factor(sample(letters[1:3], 20, replace = TRUE))
f
#>  [1] c a a b c c c c c b b b c b a a a c a a
#> Levels: a b c
fct_expand(f, "d", "e", "f")
#>  [1] c a a b c c c c c b b b c b a a a c a a
#> Levels: a b c d e f
fct_expand(f, letters[1:6])
#>  [1] c a a b c c c c c b b b c b a a a c a a
#> Levels: a b c d e f
fct_expand(f, "Z", after = 0)
#>  [1] c a a b c c c c c b b b c b a a a c a a
#> Levels: Z a b c