EVOLUTION-MANAGER
Edit File: levels.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Levels Attributes</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for levels {base}"><tr><td>levels {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Levels Attributes</h2> <h3>Description</h3> <p><code>levels</code> 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. </p> <h3>Usage</h3> <pre> levels(x) levels(x) <- value </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>an object, for example a factor.</p> </td></tr> <tr valign="top"><td><code>value</code></td> <td> <p>A valid value for <code>levels(x)</code>. For the default method, <code>NULL</code> or a character vector. For the <code>factor</code> method, a vector of character strings with length at least the number of levels of <code>x</code>, or a named list specifying how to rename the levels.</p> </td></tr> </table> <h3>Details</h3> <p>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 <code><a href="factor.html">factor</a></code>s. </p> <p>For the factor replacement method, a <code>NA</code> in <code>value</code> causes that level to be removed from the levels and the elements formerly with that level to be replaced by <code>NA</code>. </p> <p>Note that for a factor, replacing the levels via <code>levels(x) <- value</code> is not the same as (and is preferred to) <code>attr(x, "levels") <- value</code>. </p> <p>The replacement function is <a href="Primitive.html">primitive</a>. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="nlevels.html">nlevels</a></code>, <code><a href="../../stats/html/relevel.html">relevel</a></code>, <code><a href="../../stats/html/reorder.factor.html">reorder</a></code>. </p> <h3>Examples</h3> <pre> ## 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, labels = c("apple", "salad", "orange")) z levels(z) <- c("fruit", "veg", "fruit") z ## same, using a named list z <- gl(3, 2, 12, labels = c("apple", "salad", "orange")) z levels(z) <- list("fruit" = c("apple","orange"), "veg" = "salad") 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 </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>