\HeaderA{stack}{Stack or Unstack Vectors from a Data Frame or List}{stack}
\methaliasA{stack.data.frame}{stack}{stack.data.frame}
\methaliasA{stack.default}{stack}{stack.default}
\aliasA{unstack}{stack}{unstack}
\methaliasA{unstack.data.frame}{stack}{unstack.data.frame}
\methaliasA{unstack.default}{stack}{unstack.default}
\keyword{manip}{stack}
\begin{Description}\relax
Stacking vectors concatenates multiple vectors into a single vector
along with a factor indicating where each observation originated.
Unstacking reverses this operation.
\end{Description}
\begin{Usage}
\begin{verbatim}
stack(x, ...)
## Default S3 method:
stack(x, ...)
## S3 method for class 'data.frame':
stack(x, select, ...)

unstack(x, ...)
## Default S3 method:
unstack(x, form, ...)
## S3 method for class 'data.frame':
unstack(x, form = formula(x), ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] object to be stacked or unstacked
\item[\code{select}] expression, indicating variables to select from a
data frame
\item[\code{form}] 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{formula(x)}
in \code{unstack.data.frame}.
\item[\code{...}] further arguments passed to or from other methods.
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The \code{stack} 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} function reverses this operation.
\end{Details}
\begin{Value}
\code{unstack} produces a list of columns according to the formula
\code{form}.  If all the columns have the same length, the resulting
list is coerced to a data frame.

\code{stack} produces a data frame with two columns
\begin{ldescription}
\item[\code{values}] the result of concatenating the selected vectors in
\code{x}
\item[\code{ind}] a factor indicating from which vector in \code{x} the
observation originated
\end{ldescription}
\end{Value}
\begin{Author}\relax
Douglas Bates
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{lm}{lm}}, \code{\LinkA{reshape}{reshape}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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
\end{ExampleCode}
\end{Examples}

