| 42333 |
ripley |
1 |
% File src/library/utils/man/stack.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2011 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)
|
| 25360 |
ripley |
21 |
\method{stack}{default}(x, \dots)
|
|
|
22 |
\method{stack}{data.frame}(x, select, \dots)
|
|
|
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}.}
|
| 15518 |
ripley |
36 |
\item{\dots}{further arguments passed to or from other methods.}
|
| 8618 |
bates |
37 |
}
|
|
|
38 |
\details{
|
|
|
39 |
The \code{stack} function is used to transform data available as
|
|
|
40 |
separate columns in a data frame or list into a single column that can
|
|
|
41 |
be used in an analysis of variance model or other linear model. The
|
| 57267 |
ripley |
42 |
\code{unstack} function reverses this operation.
|
|
|
43 |
|
|
|
44 |
Note that \code{stack} applies to \emph{vectors} (as determined by
|
|
|
45 |
\code{\link{is.vector}}): non-vector columns (e.g., factors) will be
|
| 57269 |
ripley |
46 |
ignored (with a warning as from \R 2.15.0). Where vectors of
|
|
|
47 |
different types are selected they are concatenated by
|
|
|
48 |
\code{\link{unlist}} whose help page explains how the type of the
|
|
|
49 |
result is chosen.
|
| 57267 |
ripley |
50 |
|
|
|
51 |
These functions are generic: the supplied methods handle data frames
|
|
|
52 |
and objects coercible to lists by \code{\link{as.list}}.
|
| 8618 |
bates |
53 |
}
|
| 57267 |
ripley |
54 |
|
| 8618 |
bates |
55 |
\value{
|
|
|
56 |
\code{unstack} produces a list of columns according to the formula
|
|
|
57 |
\code{form}. If all the columns have the same length, the resulting
|
|
|
58 |
list is coerced to a data frame.
|
| 61433 |
ripley |
59 |
|
| 57267 |
ripley |
60 |
\code{stack} produces a data frame with two columns:
|
| 8618 |
bates |
61 |
\item{values}{the result of concatenating the selected vectors in
|
| 57267 |
ripley |
62 |
\code{x}.}
|
| 8618 |
bates |
63 |
\item{ind}{a factor indicating from which vector in \code{x} the
|
| 57267 |
ripley |
64 |
observation originated.}
|
| 8618 |
bates |
65 |
}
|
|
|
66 |
\author{Douglas Bates}
|
|
|
67 |
\seealso{
|
| 30461 |
ripley |
68 |
\code{\link{lm}}, \code{\link{reshape}}
|
| 8618 |
bates |
69 |
}
|
|
|
70 |
\examples{
|
| 27716 |
ripley |
71 |
require(stats)
|
| 8618 |
bates |
72 |
formula(PlantGrowth) # check the default formula
|
|
|
73 |
pg <- unstack(PlantGrowth) # unstack according to this formula
|
|
|
74 |
pg
|
|
|
75 |
stack(pg) # now put it back together
|
|
|
76 |
stack(pg, select = -ctrl) # omitting one vector
|
|
|
77 |
}
|
|
|
78 |
\keyword{manip}
|