EVOLUTION-MANAGER
Edit File: droplevels.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: Drop Unused Levels from Factors</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 droplevels {base}"><tr><td>droplevels {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Drop Unused Levels from Factors</h2> <h3>Description</h3> <p>The function <code>droplevels</code> is used to drop unused levels from a <code><a href="factor.html">factor</a></code> or, more commonly, from factors in a data frame. </p> <h3>Usage</h3> <pre> ## S3 method for class 'factor' droplevels(x, exclude = if(anyNA(levels(x))) NULL else NA, ...) ## S3 method for class 'data.frame' droplevels(x, except, exclude, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>an object from which to drop unused factor levels.</p> </td></tr> <tr valign="top"><td><code>exclude</code></td> <td> <p>passed to <code><a href="factor.html">factor</a>()</code>; factor levels which should be excluded from the result even if present. Note that this was <em>implicitly</em> <code>NA</code> in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> <= 3.3.1 which did drop <code>NA</code> levels even when present in <code>x</code>, contrary to the documentation. The current default is compatible with <code>x[ , drop=TRUE]</code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments passed to methods</p> </td></tr> <tr valign="top"><td><code>except</code></td> <td> <p>indices of columns from which <em>not</em> to drop levels</p> </td></tr> </table> <h3>Details</h3> <p>The method for class <code>"factor"</code> is currently equivalent to <code>factor(x, exclude=exclude)</code>. For the data frame method, you should rarely specify <code>exclude</code> “globally” for all factor columns; rather the default uses the same factor-specific <code>exclude</code> as the factor method itself. </p> <p>The <code>except</code> argument follow the usual indexing rules. </p> <h3>Value</h3> <p><code>droplevels</code> returns an object of the same class as <code>x</code></p> <h3>Note</h3> <p> This function was introduced in R 2.12.0. It is primarily intended for cases where one or more factors in a data frame contains only elements from a reduced level set after subsetting. (Notice that subsetting does <em>not</em> in general drop unused levels). By default, levels are dropped from all factors in a data frame, but the <code>except</code> argument allows you to specify columns for which this is not wanted. </p> <h3>See Also</h3> <p><code><a href="subset.html">subset</a></code> for subsetting data frames. <code><a href="factor.html">factor</a></code> for definition of factors. <code><a href="drop.html">drop</a></code> for dropping array dimensions. <code><a href="../../stats/html/add1.html">drop1</a></code> for dropping terms from a model. <code><a href="Extract.factor.html">[.factor</a></code> for subsetting of factors. </p> <h3>Examples</h3> <pre> aq <- transform(airquality, Month = factor(Month, labels = month.abb[5:9])) aq <- subset(aq, Month != "Jul") table( aq $Month) table(droplevels(aq)$Month) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>