Skip to contents

A 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.

Usage

SetLevels(x, levels, append_rest = TRUE)

Arguments

x

A vector (factor, character, or numeric).

levels

Character vector of desired levels, in order. Levels present in x but not listed here are appended at the end unchanged.

append_rest

Logical. If TRUE (default), unlisted levels are appended after the specified ones. If FALSE, unlisted levels are dropped (values become NA).

Value

A factor with the requested level ordering.

Details

Designed for pipe- and assignment-friendly use:


SeuratObj$Group <- SetLevels(SeuratObj$Group, c("Control", "Treated"))

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"))
} # }