quantcut.html 3 KB
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Create a Factor Variable Using the Quantiles of a Continuous Variable</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../R.css">
</head><body>

<table width="100%" summary="page for quantcut {gtools}"><tr><td>quantcut {gtools}</td><td align="right">R Documentation</td></tr></table>
<h2>Create a Factor Variable Using the Quantiles of a Continuous Variable</h2>


<h3>Description</h3>

<p>
Create a factor variable using the quantiles of a continous variable.
</p>


<h3>Usage</h3>

<pre>
quantcut(x, q=seq(0,1,by=0.25), na.rm=TRUE, ...)
</pre>


<h3>Arguments</h3>

<table summary="R argblock">
<tr valign="top"><td><code>x</code></td>
<td>
Continous variable. </td></tr>
<tr valign="top"><td><code>q</code></td>
<td>
Vector of quantiles used for creating groups. Defaults to
<code>seq(0, 1, by=0.25)</code>.  See <code><a href="../../stats/html/quantile.html">quantile</a></code> for details. </td></tr>
<tr valign="top"><td><code>na.rm</code></td>
<td>
Boolean indicating whether missing values should be
removed when computing quantiles.  Defaults to TRUE.</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
Optional arguments passed to <code><a href="../../base/html/cut.html">cut</a></code>. </td></tr>
</table>

<h3>Details</h3>

<p>
This function uses <code><a href="../../stats/html/quantile.html">quantile</a></code> to obtain the specified
quantiles of <code>x</code>, then calls <code><a href="../../base/html/cut.html">cut</a></code> to create a factor
variable using the intervals specified by these quantiles.
</p>
<p>
It properly handles cases where more than one quantile obtains the
same value, as in the second example below.  Note that in this case,
there will be fewer generated factor levels than the specified number
of quantile intervals.
</p>


<h3>Value</h3>

<p>
Factor variable with one level for each quantile interval given by <code>q</code>.</p>

<h3>Author(s)</h3>

<p>
Gregory R. Warnes <a href="mailto:warnes@bst.rochester.edu">warnes@bst.rochester.edu</a>
</p>


<h3>See Also</h3>

<p>
<code><a href="../../base/html/cut.html">cut</a></code>, <code><a href="../../stats/html/quantile.html">quantile</a></code>
</p>


<h3>Examples</h3>

<pre>

  ## create example data
  
  x &lt;- rnorm(1000)

  ## cut into quartiles
  quartiles &lt;- quantcut( x )
  table(quartiles)

  ## cut into deciles
  deciles &lt;- quantcut( x, seq(0,1,by=0.1) )
  table(deciles)

  ## show handling of 'tied' quantiles.
  x &lt;- round(x)  # discretize to create ties
  stem(x)        # display the ties
  deciles &lt;- quantcut( x, seq(0,1,by=0.1) )

  table(deciles) # note that there are only 5 groups (not 10) 
                 # due to duplicates

</pre>



<hr><div align="center">[Package <em>gtools</em> version 2.4.0 <a href="00Index.html">Index]</a></div>

</body></html>