This is a wrapper of forcats::as_factor, which sorts numeric vectors before
factoring, but levels character vectors in order of appearance.
Arguments
- data
vector or data.frame column
- unique.n
threshold to convert class to factor
Examples
sample(seq_len(4), 20, TRUE) |>
var2fct(6) |>
summary()
#> 1 2 3 4
#> 9 2 6 3
sample(letters, 20) |>
var2fct(6) |>
summary()
#> Length Class Mode
#> 20 character character
sample(letters[1:4], 20, TRUE) |> var2fct(6)
#> [1] c d c d a b d a d c a d a c c a b c d c
#> Levels: c d a b