How to Stack DataFrame Columns in R?
Last Updated :
19 Dec, 2021
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.
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.
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
Method 2: Using melt method
The reshape2 package in R can be used to change the structure of the data supplied and can be installed and imported into the working space using the following command :
install.packages("reshape2")
library(reshape2)
The melt method in this package can be used to stack data frame columns together. It is used to reshape and elongate the data frame. The melt() method has the following syntax :
melt(data, id.var , variable.name)
Arguments :
- data - The data frame to stack columns of
- id.ar - The columns to use as primary key
- variable.name - The new column name to append
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
# importing the required library
library("reshape2")
# 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 <- reshape2::melt(data, id.var = c('col1', 'semester'),
variable.name = 'quiz_marks')
print(data_mod)
Output
[1] "Modified DataFrame"
col1 semester quiz_marks value
1 Yash A quiz_sst 1
2 Yash B quiz_sst 3
3 Mallika A quiz_sst 4
4 Mallika B quiz_sst 8
5 Muskan A quiz_sst 9
6 Muskan B quiz_sst 1
7 Yash A quiz_maths 2
8 Yash B quiz_maths 4
9 Mallika A quiz_maths 6
10 Mallika B quiz_maths 2
11 Muskan A quiz_maths 7
12 Muskan B quiz_maths 3
Similar Reads
How to Switch Two Columns in R DataFrame? In this article, we will discuss how to switch two columns in dataframe in R Programming Language. Let's create the dataframe with 6 columns R # create a dataframe data = data.frame(column1=c(1, 2, 3), column2=c(4, 5, 6), column3=c(2, 3, 4), column4=c(4, 5, 6), column5=c(5, 3, 2), column6=c(2, 3, 1)
1 min read
How to add column to dataframe in R ? In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n)Â Here c() function is a vec
2 min read
How to Sort a DataFrame in R ? In this article, we will discuss how to sort the dataframe in R Programming Language. In R DataFrame is a two-dimensional tabular data structure that consists of rows and columns. Sorting a DataFrame allows us to reorder the rows based on the values in one or more columns. This can be useful for var
5 min read
How to split DataFrame in R In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r
4 min read
How to Add an Empty Column to DataFrame in R? In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe  is the inpu
1 min read
How to add suffix to column names in R DataFrame ? Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read