6.2 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Stack Data Frame Columns in R

How to Stack Data Frame Columns in R

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories