How to combine multiple R data frames that contains one common column?


To combine multiple R data frames that contains one common column, we can follow the below steps −

  • First of all, create a number of data frames.
  • Then, use join_all function from plyr package to combine the data frames.

Create the data frame

Let's create a data frame as shown below −

 Live Demo

> x<-sample(LETTERS[1:4],10,replace=TRUE)
> y1<-rpois(10,5)
> df1<-data.frame(x,y1)
> df1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   x y1
1  A  6
2  B 10
3  A  4
4  C  5
5  C  3
6  C  6  
7  B  2
8  B 10
9  D  1
10 D  3

Let’s create a data frame df2 as shown below −

 Live Demo

> x<-sample(LETTERS[1:10],10)
> y2<-rpois(10,5)
> df2<-data.frame(x,y2)
> df2

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   x y2
1  E  2
2  A  7
3  I  0
4  H  6
5  C  4
6  D 10
7  J  3
8  G  5
9  F  4
10 B  5 

Let’s create a data frame df3 as shown below −

> x<-sample(LETTERS[1:5],10,replace=TRUE)
> y3<-rpois(10,2)
> df3<-data.frame(x,y3)
> df3

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   x  y3
1  C 1
2  C 4
3  A 2
4  C 1
5  C 1
6  B 0
7  D 2
8  D 3
9  D 4
10 D 0

Let’s create a data frame df4 as shown below −

 Live Demo

> x<-sample(LETTERS[1:8],10,replace=TRUE)
> y4<-rpois(10,8)
> df4<-data.frame(x,y4)
> df4

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

  x y4
1 G 7
2 B 9
3 G 6
4 G 8
5 G 9
6 F 11
7 H 10
8 D 10
9 E 7
10 E 9

Combine all the data frames

Using join_all function by listing the data frames with list function to combine them −

> library(plyr)
> df<-join_all(list(df1,df2,df3,df4),by="x",type="inner")
> df
   x y1 y2 y3 y4
1  B 10 5 0  9
2  B 2  5 0  9
3  B 10 5 0  9
4  D 1 10 2 10
5  D 1 10 3 10
6  D 1 10 4 10
7  D 1 10 0 10
8  D 3 10 2 10
9  D 3 10 3 10
10 D 3 10 4 10
11 D 3 10 0 10

Updated on: 13-Aug-2021

490 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements