The R Project SVN R

Rev

Rev 30448 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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