factor(x) orders levels alphabetically, which silently
decides the reference category of every downstream regression.
This constructor requires the level set to be written out, errors
on values outside it, and records the declared reference level.
Usage
morie_safe_factor(x, levels, reference = NULL)
Arguments
- x
Character (or factor) vector.
- levels
Complete character vector of allowed levels, in the
intended order — the FIRST is the reference category.
- reference
Optional; assert which level is the reference
(must equal levels[1]).
Value
A factor with exactly the declared levels.
Examples
morie_safe_factor(c("White", "Black", "White"),
levels = c("White", "Black"))
#> [1] White Black White
#> Levels: White Black