The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8618 bates 1
\name{stack}
2
\alias{stack}
3
\alias{stack.default}
4
\alias{stack.data.frame}
5
\alias{unstack}
6
\alias{unstack.default}
7
\alias{unstack.data.frame}
8
\title{Stack or Unstack Vectors from a Data Frame or List}
9
\description{
10
  Stacking vectors concatenates multiple vectors into a single vector
11
  along with a factor indicating where each observation originated.
12
  Unstacking reverses this operation.
13
}
14
\usage{
15
stack(x, \dots)
25360 ripley 16
\method{stack}{default}(x, \dots)
17
\method{stack}{data.frame}(x, select, \dots)
18
 
8618 bates 19
unstack(x, \dots)
25360 ripley 20
\method{unstack}{default}(x, form, \dots)
21
\method{unstack}{data.frame}(x, form = formula(x), \dots)
8618 bates 22
}
23
\arguments{
24
  \item{x}{object to be stacked or unstacked}
25
  \item{select}{expression, indicating variables to select from a
26
    data frame}
27
  \item{form}{a two-sided formula whose left side evaluates to the
28
    vector to be unstacked and whose right side evaluates to the
29
    indicator of the groups to create.  Defaults to \code{formula(x)}
30
    in \code{unstack.data.frame}.}
15518 ripley 31
  \item{\dots}{further arguments passed to or from other methods.}
8618 bates 32
}
33
\details{
34
  The \code{stack} function is used to transform data available as
35
  separate columns in a data frame or list into a single column that can
36
  be used in an analysis of variance model or other linear model.  The
17330 tlumley 37
  \code{unstack} function reverses this operation. 
8618 bates 38
}
39
\value{
40
  \code{unstack} produces a list of columns according to the formula
41
  \code{form}.  If all the columns have the same length, the resulting
42
  list is coerced to a data frame.
43
 
44
  \code{stack} produces a data frame with two columns
45
  \item{values}{the result of concatenating the selected vectors in
46
    \code{x}}
47
  \item{ind}{a factor indicating from which vector in \code{x} the
48
    observation originated}
49
}
50
\author{Douglas Bates}
51
\seealso{
30461 ripley 52
  \code{\link{lm}}, \code{\link{reshape}}
8618 bates 53
}
54
\examples{
27716 ripley 55
require(stats)
8618 bates 56
formula(PlantGrowth)         # check the default formula
57
pg <- unstack(PlantGrowth)   # unstack according to this formula
58
pg
59
stack(pg)                    # now put it back together
60
stack(pg, select = -ctrl)    # omitting one vector
61
}
62
\keyword{manip}