levels

The levels of a factor. These can be counted with nlevels and reordered if required.

x = factor(letters[1:5])
levels(x)
# [1] "a" "b" "c" "d" "e"
nlevels(x)
# [1] 5

Assign individual levels
x <- gl(2, 4, 8)
levels(x)[1] <- "low"
levels(x)[2] <- "high"
x

Or as a group

y <- gl(2, 4, 8)
levels(y) <- c("low", "high")
y

Combine some levels

z <- gl(3, 2, 12)
levels(z) <- c("A", "B", "A")
z

Same, using a named list

z <- gl(3, 2, 12)
levels(z) <- list(A=c(1,3), B=2)
z

We can add levels this way:

f <- factor(c("a","b"))
levels(f) <- c("c", "a", "b")
f

f <- factor(c("a","b"))
levels(f) <- list(C="C", A="a", B="b")
f