levels provides access to the levels attribute of a variable. The first form returns the value of the levels of its argument and the second sets the attribute.
levels(x) levels(x) <- value
Both the extractor and replacement forms are generic and new methods can be written for them. The most important method for the replacement function is that for
factors. For the factor replacement method, a
value causes that level to be removed from the levels and the elements formerly with that level to be replaced by
The replacement function is primitive.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
## assign individual levels x <- gl(2, 4, 8) levels(x) <- "low" levels(x) <- "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
Documentation reproduced from R 2.15.3. License: GPL-2.