Home » How to Stack Data Frame Columns in R

How to Stack Data Frame Columns in R

by Erma Khan

Often you may want to stack two or more data frame columns into one column in R.

For example, you may want to go from this:

  person trial outcome1 outcome2
     A     1        7        4
     A     2        6        4
     B     1        6        5
     B     2        5        5
     C     1        4        3
     C     2        4        2

To this:

   person trial outcomes  value
      A     1   outcome1     7
      A     2   outcome1     6
      B     1   outcome1     6
      B     2   outcome1     5
      C     1   outcome1     4
      C     2   outcome1     4
      A     1   outcome2     4
      A     2   outcome2     4
      B     1   outcome2     5
      B     2   outcome2     5
      C     1   outcome2     3
      C     2   outcome2     2

This tutorial explains two methods you can use in R to do this.

Method 1: Use the Stack Function in Base R

The following code shows how to stack columns using the stack function in base R:

#create original data frame
data #stack the third and fourth columns
cbind(data[1:2], stack(data[3:4]))

   person trial values      ind
1       A     1      7 outcome1
2       A     2      6 outcome1
3       B     1      6 outcome1
4       B     2      5 outcome1
5       C     1      4 outcome1
6       C     2      4 outcome1
7       A     1      4 outcome2
8       A     2      4 outcome2
9       B     1      5 outcome2
10      B     2      5 outcome2
11      C     1      3 outcome2
12      C     2      2 outcome2

Method 2: Use the Melt Function from Reshape2

The following code shows how to stack columns using the melt function from the reshape2 library:

#load library
library(reshape2)

#create original data frame
data #melt columns of data frame
melt(data, id.var = c('person', 'trial'), variable.name = 'outcomes')

   person trial outcomes value
1       A     1 outcome1     7
2       A     2 outcome1     6
3       B     1 outcome1     6
4       B     2 outcome1     5
5       C     1 outcome1     4
6       C     2 outcome1     4
7       A     1 outcome2     4
8       A     2 outcome2     4
9       B     1 outcome2     5
10      B     2 outcome2     5
11      C     1 outcome2     3
12      C     2 outcome2     2

You can find the complete documentation for the melt function here.

Additional Resources

How to Switch Two Columns in R
How to Rename Columns in R
How to Sum Specific Columns in R

Related Posts