Combine two DataFrames in R with different columns
Last Updated :
23 May, 2021
In this article, we will discuss how to combine two dataframes with different columns in R Programming Language.
Method 1 : 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 data frames with different columns. The column names are number may be different in the input data frames. Missing columns of the corresponding data frames are filled with NA. The output data frame contains a column only if it is present in any of the data frame.
Syntax:
rbind.fill( df1, df2)
The following properties are maintained by the rbind.fill() method :
- The data frames are appended in the order of their specification in the function.
- The total number of columns is equivalent to the summation of a number of columns of both the data frames.
- The total number of rows are equivalent to the summation of number of rows of both the data frames.
- The appearance of columns is in the order of data frame arguments declaration during function call.
- Empty cells are created in the missing columns.
Example:
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
[1] "First Dataframe"
col1 col2 col3 col4
1 2 4 8 A
2 4 6 10 B
3 6 8 12 C
[1] "Second Dataframe"
col4 col5
1 a TRUE
2 b TRUE
3 c TRUE
4 d TRUE
[1] "Combining Dataframe"
[1] "First Dataframe"
col1 col2 col3 col4
1 2 4 8 20
2 4 6 10 16
3 6 8 12 14
[1] "Second Dataframe"
col5 col6
1 a TRUE
2 b TRUE
3 c TRUE
4 d TRUE
[1] "Combining Dataframe"
col1 col2 col3 col4 col5
1 2 4 8 A NA
2 4 6 10 B NA
3 6 8 12 C NA
4 NA NA NA a TRUE
5 NA NA NA b TRUE
6 NA NA NA c TRUE
7 NA NA NA d TRUE
Method 2: Using dplyr package
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 data frames with different columns. The column names are number may be different in the input data frames. Missing columns of the corresponding data frames are filled with NA. The output data frame contains a column only if it is present in any of the data frame.
Syntax:
bind_rows(df1, df2)
The following properties are maintained by the bind_rows() method :
- The data frames are appended in the order of their specification in the function.
- The total number of columns are equivalent to the summation of number of columns of both the data frames.
- The total number of rows are equivalent to the summation of number of rows of both the data frames.
Example:
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
[1] "First Dataframe"
col1 col2 col3 col4
1 2 4 8 20
2 4 6 10 16
3 6 8 12 14
[1] "Second Dataframe"
col5 col6
1 a TRUE
2 b TRUE
3 c TRUE
4 d TRUE
[1] "Combining Dataframe"
col1 col2 col3 col4 col5 col6
1 2 4 8 20 <NA> NA
2 4 6 10 16 <NA> NA
3 6 8 12 14 <NA> NA
4 NA NA NA NA a TRUE
5 NA NA NA NA b TRUE
6 NA NA NA NA c TRUE
7 NA NA NA NA d TRUE
In case, any of the column name is same in both of the input data frames, then the following properties are encountered :
- The class of the common column should be same in both the data frames, otherwise an error is encountered.
- In this case, the total number of columns in the output data frame should be equivalent to the total input columns minus the intersecting columns.
Example:
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
[1] "First Dataframe"
col1 col2 col3 col4
1 2 4 8 A
2 4 6 10 B
3 6 8 12 C
[1] "Second Dataframe"
col4 col5
1 a TRUE
2 b TRUE
3 c TRUE
4 d TRUE
[1] "Combining Dataframe"
[1] "First Dataframe"
col1 col2 col3 col4
1 2 4 8 20
2 4 6 10 16
3 6 8 12 14
[1] "Second Dataframe"
col5 col6
1 a TRUE
2 b TRUE
3 c TRUE
4 d TRUE
[1] "Combining Dataframe"
col1 col2 col3 col4 col5
1 2 4 8 A NA
2 4 6 10 B NA
3 6 8 12 C NA
4 NA NA NA a TRUE
5 NA NA NA b TRUE
6 NA NA NA c TRUE
7 NA NA NA d TRUE
Similar Reads
Convert DataFrame to Matrix with Column Names in R
Data frames and matrices are R objects, both of which allow tabular storage of the data into well organized cells. However, the data in a data frame can consist of different data types, that is the cells may contain data belonging to a combination of data types. Matrices, on the other hand, strictly
3 min read
How to Compare Two Columns in R DataFrame?
In this article, we will discuss how to compare two columns in the dataframe through R Programming Language. We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly. Syntax: ifelse(df$column1 > df$column2, statement,..
2 min read
Convert dataframe column to list in R
In this article, we will learn how to convert a dataframe into a list by columns in R Programming language. We will be using as.list() function, this function is used to convert an object to a list. These objects can be Vectors, Matrices, Factors, and data frames. Syntax: as.list( object ) Parameter
2 min read
Convert list to dataframe with specific column names in R
A list contains different types of objects as their components. The components may belong to different data types or different dimensions. Vector can be useful components of a list and can be easily mapped as the rows or columns of a dataframe. Each column in the dataframe is referenced using a uniq
4 min read
Convert dataframe rows and columns to vector in R
In this article, we are going to convert a dataframe column to a vector and a dataframe row to a vector in the R Programming Language. Convert dataframe columns into vectors We are taking a column in the data frame and passing it into another variable by using the selection method. The selection met
2 min read
Filter DataFrame columns in R by given condition
In this article, we are going to see how to select DataFrame columns in R Programming Language by given condition. R data frame columns can be subjected to constraints, and produce smaller subsets. However, while the conditions are applied, the following properties are maintained : Rows of the data
5 min read
Create DataFrame with Spaces in Column Names in R
In this article, we will see how to create a DataFrame with spaces in column names in R Programming Language. Method 1: Using check.names attribute The data.frame() method in R can be used to create a data frame with individual rows and columns in R. This method contains an attribute check.names, wh
4 min read
Convert Row Names into Column of DataFrame in R
In this article, we will discuss how to Convert Row Names into Columns of Dataframe in R Programming Language. Method 1: Using row.names() row.name() function is used to set and get the name of the DataFrame. Apply the row.name() function to the copy of the DataFrame and a name to the column which c
3 min read
Create empty DataFrame with only column names in R
In this article, we are going to discuss how to create a data frame in r with column names and create an empty data frame with column names in the R Programming Language. The basic syntax for creating a data frame is using data. frame(). Create a Data Frame with Values and column Names C/C++ Code #
3 min read
Convert two columns of a data frame to a named vector in R
In this article, we will discuss how to convert two columns of a dataframe to a named vector in R Programming Language. Let see the implementation stepwise: Example 1: Creating dataframe and convert columns to vector. Step 1: Here we create a DataFrame with name data. There is a total of two columns
2 min read