
Relevel a factor or vector concisely
SetLevels.RdA drop-in replacement for the verbose
factor(x, levels = c(...)) pattern. Converts non-factors
automatically (with a message), places any listed levels first in the
specified order, and appends all remaining levels at the end - so you only
need to list the levels you care about.
Arguments
- x
A vector (factor, character, or numeric).
- levels
Character vector of desired levels, in order. Levels present in
xbut not listed here are appended at the end unchanged.- append_rest
Logical. If
TRUE(default), unlisted levels are appended after the specified ones. IfFALSE, unlisted levels are dropped (values becomeNA).
Examples
if (FALSE) { # \dontrun{
# Reorder two key levels; remaining levels appended automatically
SeuratObj$Group <- SetLevels(SeuratObj$Group, c("Control", "Treated"))
# Full explicit ordering
SeuratObj$Dementia <- SetLevels(SeuratObj$Dementia,
c("Reference.Control", "No dementia.Control", "Dementia.Control",
"No dementia.AD", "Dementia.AD"))
} # }