How to use stack method In R Language

The cbind() operation is used to stack the columns of the data frame together. Initially, the first two columns of the data frame are combined together using the df[1:2]. This is followed by the application of stack() method applied on the last two columns. 

The stack method in base R is used to transform data available in the form of separate columns within a data frame or a list into a single column. The stack method produces a result in the form of a data frame with two columns:

  • values: the result produced by concatenating the selected vectors in x.
  • ind: a factor indicating from which vector in x the observation originated.

Syntax:

stack(x)

Arguments : 

  • x – a list or data frame to be stacked

Original Data frame looks as:

     col1 semester quiz_sst quiz_maths
1    Yash        A        1          2
2    Yash        B        3          4
3 Mallika        A        4          6
4 Mallika        B        8          2
5  Muskan        A        9          7
6  Muskan        B        1          3

R




# creating a data frame
data <- data.frame(col1=c('Yash', 'Yash', 'Mallika'
                          'Mallika', 'Muskan', 'Muskan'),
                   semester=c(rep(LETTERS[1:2],3)),
                   quiz_sst=c(1, 3, 4, 8, 9, 1),
                   quiz_maths=c(2, 4, 6, 2, 7, 3))
  
# binding the first two columns as it is 
# and stacking the third and fourth columns
data_mod <- cbind(data[1:2], stack(data[3:4]))
print(data_mod)


Output

     col1 semester  values        ind
1     Yash        A      1   quiz_sst
2     Yash        B      3   quiz_sst
3  Mallika        A      4   quiz_sst
4  Mallika        B      8   quiz_sst
5   Muskan        A      9   quiz_sst
6   Muskan        B      1   quiz_sst
7     Yash        A      2 quiz_maths
8     Yash        B      4 quiz_maths
9  Mallika        A      6 quiz_maths
10 Mallika        B      2 quiz_maths
11  Muskan        A      7 quiz_maths
12  Muskan        B      3 quiz_maths

How to Stack DataFrame Columns in R?

A dataframe is a tubular structure composed of rows and columns. The dataframe columns can be stacked together to divide the columns depending on the values contained within them. 

Similar Reads

Method 1: Using stack method

The cbind() operation is used to stack the columns of the data frame together. Initially, the first two columns of the data frame are combined together using the df[1:2]. This is followed by the application of stack() method applied on the last two columns....

Method 2: Using melt method

...

Contact Us