EVOLUTION-MANAGER
Edit File: stack.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: Stack or Unstack Vectors from a Data Frame or List</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 stack {utils}"><tr><td>stack {utils}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Stack or Unstack Vectors from a Data Frame or List</h2> <h3>Description</h3> <p>Stacking vectors concatenates multiple vectors into a single vector along with a factor indicating where each observation originated. Unstacking reverses this operation. </p> <h3>Usage</h3> <pre> stack(x, ...) ## Default S3 method: stack(x, drop=FALSE, ...) ## S3 method for class 'data.frame' stack(x, select, drop=FALSE, ...) unstack(x, ...) ## Default S3 method: unstack(x, form, ...) ## S3 method for class 'data.frame' unstack(x, form, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a list or data frame to be stacked or unstacked.</p> </td></tr> <tr valign="top"><td><code>select</code></td> <td> <p>an expression, indicating which variable(s) to select from a data frame.</p> </td></tr> <tr valign="top"><td><code>form</code></td> <td> <p>a two-sided formula whose left side evaluates to the vector to be unstacked and whose right side evaluates to the indicator of the groups to create. Defaults to <code><a href="../../stats/html/formula.html">formula</a>(x)</code> in the data frame method for <code>unstack</code>.</p> </td></tr> <tr valign="top"><td><code>drop</code></td> <td> <p>Whether to drop the unused levels from the “ind” column of the return value. </p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further arguments passed to or from other methods.</p> </td></tr> </table> <h3>Details</h3> <p>The <code>stack</code> function is used to transform data available as separate columns in a data frame or list into a single column that can be used in an analysis of variance model or other linear model. The <code>unstack</code> function reverses this operation. </p> <p>Note that <code>stack</code> applies to <em>vectors</em> (as determined by <code><a href="../../base/html/vector.html">is.vector</a></code>): non-vector columns (e.g., factors) will be ignored with a warning. Where vectors of different types are selected they are concatenated by <code><a href="../../base/html/unlist.html">unlist</a></code> whose help page explains how the type of the result is chosen. </p> <p>These functions are generic: the supplied methods handle data frames and objects coercible to lists by <code><a href="../../base/html/list.html">as.list</a></code>. </p> <h3>Value</h3> <p><code>unstack</code> produces a list of columns according to the formula <code>form</code>. If all the columns have the same length, the resulting list is coerced to a data frame. </p> <p><code>stack</code> produces a data frame with two columns: </p> <table summary="R valueblock"> <tr valign="top"><td><code>values</code></td> <td> <p>the result of concatenating the selected vectors in <code>x</code>.</p> </td></tr> <tr valign="top"><td><code>ind</code></td> <td> <p>a factor indicating from which vector in <code>x</code> the observation originated.</p> </td></tr> </table> <h3>Author(s)</h3> <p>Douglas Bates</p> <h3>See Also</h3> <p><code><a href="../../stats/html/lm.html">lm</a></code>, <code><a href="../../stats/html/reshape.html">reshape</a></code> </p> <h3>Examples</h3> <pre> require(stats) formula(PlantGrowth) # check the default formula pg <- unstack(PlantGrowth) # unstack according to this formula pg stack(pg) # now put it back together stack(pg, select = -ctrl) # omitting one vector </pre> <hr /><div style="text-align: center;">[Package <em>utils</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>