# stack

##### Stack or Unstack Vectors from a Data Frame or List

Stacking vectors concatenates multiple vectors into a single vector along with a factor indicating where each observation originated. Unstacking reverses this operation.

- Keywords
- manip

##### Usage

```
stack(x, ...)
## S3 method for class 'default':
stack(x, \dots)
## S3 method for class 'data.frame':
stack(x, select, \dots)
```unstack(x, ...)
## S3 method for class 'default':
unstack(x, form, \dots)
## S3 method for class 'data.frame':
unstack(x, form, \dots)

##### Arguments

- x
- a list or data frame to be stacked or unstacked.
- select
- an expression, indicating which variable(s) to select from a data frame.
- 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
`formula(x)`

in the data frame method for`unstack`

. - ...
- further arguments passed to or from other methods.

##### Details

The `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
`unstack`

function reverses this operation.

Note that `stack`

applies to *vectors* (as determined by
`is.vector`

): non-vector columns (e.g., factors) will be
ignored (with a warning as from R2.15.0). Where vectors of
different types are selected they are concatenated by
`unlist`

whose help page explains how the type of the
result is chosen.

These functions are generic: the supplied methods handle data frames
and objects coercible to lists by `as.list`

.

##### Value

`unstack`

produces a list of columns according to the formula`form`

. If all the columns have the same length, the resulting list is coerced to a data frame.`stack`

produces a data frame with two columns:values the result of concatenating the selected vectors in `x`

.ind a factor indicating from which vector in `x`

the observation originated.

##### See Also

##### Examples

`library(utils)`

```
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
```

*Documentation reproduced from package utils, version 3.3, License: Part of R 3.3*