Change the reference level for a factor
You want to reorder the levels of a factor to ensure that a specific level comes first
Solution: By default, R orders factor levels according to their alphabetical/lexicographic order. This is especially useful for models, when the first factor levels is taken as the reference level.
relevel function sets the first level of an
unordered factor. It takes the factor as first argument, and the
ref argument expects a string which sets the factor level
that is to be first.
> f <- factor(LETTERS[1:5]) > f  A B C D E Levels: A B C D E > f <- relevel(f, ref="D") # Set level D as reference > f  A B C D E Levels: D A B C E
Relevel function from the
package can set the order of each of the factor levels and not just
the first level.
Back to tips.