- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
> 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 −
> 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 −
> 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
- Related Articles
- How to combine multiple R data frames stored in multiple lists based on a common column?
- How to combine multiple columns into one in R data frame without using column names?
- How to combine lists of data frames in R?
- How to combine data frames stored in a list in R?
- How to Combine Data from Multiple Excel Worksheets into One?
- How to combine two data frames by filling NA in empty places in R?\n
- How to remove row that contains maximum for each column in R data frame?
- How to convert a data frame column to date that contains integer values in R?
- How to match and replace column names stored in R data frames in R-Programming?
- How to extract the column from a list of data frames in R?
- How to remove a column from a data frame that contains same value in R?
- How to subset rows that contains maximum depending on another column in R data frame?
- How to remove rows from an R data frame that contains at least one NaN?
- How to combine year, month, and day column in an R data frame?
- How to create a column of total in data frames stored in R list?
