The R Project SVN R

Rev

Rev 70857 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/utils/man/stack.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
71883 ripley 3
% Copyright 1995-2017 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
8618 bates 6
\name{stack}
56186 murdoch 7
\alias{stack}
8618 bates 8
\alias{stack.default}
9
\alias{stack.data.frame}
10
\alias{unstack}
11
\alias{unstack.default}
12
\alias{unstack.data.frame}
13
\title{Stack or Unstack Vectors from a Data Frame or List}
14
\description{
15
  Stacking vectors concatenates multiple vectors into a single vector
16
  along with a factor indicating where each observation originated.
17
  Unstacking reverses this operation.
18
}
19
\usage{
20
stack(x, \dots)
70857 lawrence 21
\method{stack}{default}(x, drop=FALSE, \dots)
22
\method{stack}{data.frame}(x, select, drop=FALSE, \dots)
25360 ripley 23
 
8618 bates 24
unstack(x, \dots)
25360 ripley 25
\method{unstack}{default}(x, form, \dots)
40337 ripley 26
\method{unstack}{data.frame}(x, form, \dots)
8618 bates 27
}
28
\arguments{
57267 ripley 29
  \item{x}{a list or data frame to be stacked or unstacked.}
30
  \item{select}{an expression, indicating which variable(s) to select from a
31
    data frame.}
8618 bates 32
  \item{form}{a two-sided formula whose left side evaluates to the
33
    vector to be unstacked and whose right side evaluates to the
57269 ripley 34
    indicator of the groups to create.  Defaults to \code{\link{formula}(x)}
57267 ripley 35
    in the data frame method for \code{unstack}.}
70857 lawrence 36
  \item{drop}{Whether to drop the unused levels from the \dQuote{ind}
37
    column of the return value.
38
  }
15518 ripley 39
  \item{\dots}{further arguments passed to or from other methods.}
8618 bates 40
}
41
\details{
42
  The \code{stack} function is used to transform data available as
43
  separate columns in a data frame or list into a single column that can
44
  be used in an analysis of variance model or other linear model.  The
57267 ripley 45
  \code{unstack} function reverses this operation.
46
 
47
  Note that \code{stack} applies to \emph{vectors} (as determined by
48
  \code{\link{is.vector}}): non-vector columns (e.g., factors) will be
71883 ripley 49
  ignored with a warning.  Where vectors of different types are selected
50
  they are concatenated by \code{\link{unlist}} whose help page explains
51
  how the type of the result is chosen.
57267 ripley 52
 
53
  These functions are generic: the supplied methods handle data frames
54
  and objects coercible to lists by \code{\link{as.list}}.
8618 bates 55
}
57267 ripley 56
 
8618 bates 57
\value{
58
  \code{unstack} produces a list of columns according to the formula
59
  \code{form}.  If all the columns have the same length, the resulting
60
  list is coerced to a data frame.
61433 ripley 61
 
57267 ripley 62
  \code{stack} produces a data frame with two columns:
8618 bates 63
  \item{values}{the result of concatenating the selected vectors in
57267 ripley 64
    \code{x}.}
8618 bates 65
  \item{ind}{a factor indicating from which vector in \code{x} the
57267 ripley 66
    observation originated.}
8618 bates 67
}
68
\author{Douglas Bates}
69
\seealso{
30461 ripley 70
  \code{\link{lm}}, \code{\link{reshape}}
8618 bates 71
}
72
\examples{
27716 ripley 73
require(stats)
8618 bates 74
formula(PlantGrowth)         # check the default formula
75
pg <- unstack(PlantGrowth)   # unstack according to this formula
76
pg
77
stack(pg)                    # now put it back together
78
stack(pg, select = -ctrl)    # omitting one vector
79
}
80
\keyword{manip}