How to add dataframe to dataframe in R ?
Last Updated :
13 Jul, 2021
In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language.
Method 1: Using rbind() method
The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different number of rows. The class of the dataframe columns should be consistent with each other, otherwise, errors are thrown. The following properties are maintained :
- The rows are appended in the order of dataframe appearance during the function call.
- The total number of columns is equivalent to the columns in any of the dataframes.
- The total number of rows is equivalent to the summation of rows in the dataframe.
rbind ( df1, df2)
Example: Adding Dataframe using rbind() method
R
data_frame1 <- data.frame (col1 = c (2, 4, 6),
col2 = FALSE ,
col3 = LETTERS [1 : 3])
print ( "First Dataframe" )
print (data_frame1)
data_frame2 <- data.frame (col1 = c (1 , 3),
col2 = TRUE ,
col3 = LETTERS [4 : 5])
print ( "Second Dataframe" )
print (data_frame2)
print ( "Combining Dataframe" )
rbind (data_frame1, data_frame2)
|
Output:

Method 2: Using plyr package
The “plyr” package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the following command:
install.packages("plyr")
rbind.fill() method in R is an enhancement of the rbind() method in base R, is used to combine dataframes with different columns. The column names are numbers may be different in the input dataframes. Missing columns of the corresponding dataframes are filled with NA. The output dataframe contains a column only if it is present in any of the dataframes.
rbind.fill( df1, df2)
The following properties are maintained by the rbind.fill() method :
- The dataframes are appended in the order of their specification in the function.
- The total number of columns is equivalent to the summation of the number of columns of both the dataframes.
- The total number of rows is equivalent to the summation of the number of rows of both the dataframes.
- The appearance of columns is in the order of dataframe arguments declaration during the function call.
- Empty cells are created in the missing columns.
Example: Adding Dataframe using plyr package
R
library ( "plyr" )
data_frame1 <- data.frame (col1 = c (2, 4, 6),
col2 = c (4, 6, 8),
col3 = c (8, 10, 12),
col4 = LETTERS [1 : 3])
print ( "First Dataframe" )
print (data_frame1)
data_frame2 <- data.frame (col4 = letters [1:4],
col5 = TRUE )
print ( "Second Dataframe" )
print (data_frame2)
print ( "Combining Dataframe" )
rbind.fill (data_frame1, data_frame2)
|
Output:
The “dplyr” package in R is used to work with data, including its enhancements and manipulations. It can be loaded and installed into the working space by the following command :
install.packages("dplyr")
The bind_rows() method is used to combine dataframes with different columns. The column names are numbers may be different in the input dataframes. Missing columns of the corresponding dataframes are filled with NA. The output dataframe contains a column only if it is present in any of the dataframes.
bind_rows(df1, df2)
The properties are similar to the working of the “plyr” package. This method just offers an enhancement to data manipulation.
Example: Adding Dataframe using dplyr package
R
library ( "dplyr" )
data_frame1 <- data.frame (col1 = c (2, 4, 6),
col2 = c (4, 6, 8),
col3 = c (8, 10, 12),
col4 = c (20, 16, 14))
print ( "First Dataframe" )
print (data_frame1)
data_frame2 <- data.frame (col5 = letters [1 : 4],
col6 = TRUE )
print ( "Second Dataframe" )
print (data_frame2)
print ( "Combining Dataframe" )
bind_rows (data_frame1, data_frame2)
|
Output:
In case, any of the column names are the same in both of the input dataframes, then the following properties are encountered :
- The class of the common column should be the same in both the dataframes, otherwise, an error is encountered.
In this case, the total number of columns in the output dataframe should be equivalent to the total input columns minus the intersecting columns.
Example: Adding dataframe with one column having same name.
R
library ( "dplyr" )
data_frame1 <- data.frame (col1 = c (2, 4, 6),
col2 = c (4, 6, 8),
col3 = c (8, 10, 12),
col4 = LETTERS [1 : 3])
print ( "First Dataframe" )
print (data_frame1)
data_frame2 <- data.frame (col4 = letters [1 : 4],
col5 = TRUE )
print ( "Second Dataframe" )
print (data_frame2)
print ( "Combining Dataframe" )
bind_rows (data_frame1,data_frame2)
|
Output:
Similar Reads
How to add Header to Dataframe in R ?
A header necessarily stores the names or headings for each of the columns. It basically helps the user to identify the role of the respective column in the data frame. The top row containing column names is called the header row of the data frame. In this article, we will learn how to add a Header t
3 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 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 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 add a row to R dataframe ?
In this article, we will see how to add rows to a DataFrame in R Programming Language. To do this we will use rbind() function. This function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. Syntax: rbind(dataframe 1, dataframe 2) Example 1 : [GFGTABS] R # creating a
2 min read
How to Transpose a Data Frame in R?
Transposing means converting rows to columns and columns to rows of a data frame. Transposing can be useful for various purposes, such as reshaping data or preparing it for specific analyses. Transpose a Data Frame in R Using t() functionHere we are using t() function which stands for transpose to T
3 min read
Add Index ID to DataFrame in R
In this article, we will discuss how index ID can be added to dataframes in the R programming language. Method 1 : Using cbind() and nrow() methods The nrow() method in R Programming language is used to compute the number of rows in the dataframe that is specified as an argument of this method. cbi
5 min read
How to create a DataFrame from given vectors in R ?
In this article we will see how to create a Dataframe from four given vectors in R. To create a data frame in R using the vector, we must first have a series of vectors containing data. The data.frame() function is used to create a data frame from vector in R. Syntax: data.frame(vectors) Example 1.
2 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 append rows to R DataFrame ?
In this article, let's discuss how to append rows to DataFrame in R Programming Language. There are various ways to append rows to an R data frame :Â Method 1: Using rbind() A row can be defined using a vector of values and appended to the data frame using the rbind() method, which essentially means
3 min read