
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Join Two Data Frames Based on One Factor Column in R using dplyr
When there is a common factor with different levels, the joining of data frames is possible but the result will present all the levels with dplyr. We can make use of left_join function to join the two data frames but the size of the first data frame must be greater than the second data frame if they are not same.
Example
Consider the below data frames −
> Class<-c("Statistics","Maths","Chemistry","Physics","Economics","Political Science", + "Geography") > df1<-data.frame(Class) > df1 Class 1 Statistics 2 Maths 3 Chemistry 4 Physics 5 Economics 6 Political Science 7 Geography > Subject<-c("Maths","Chemistry","Physics","Economics","Political Science", + "Geography") > Age<-c(18,21,22,25,21,23) > df2<-data.frame(Subject,Age) > df2 Subject Age 1 Maths 18 2 Chemistry 21 3 Physics 22 4 Economics 25 5 Political Science 21 6 Geography 23
In these two data frames the levels of factor Class and Subject are same therefore we can use them to join the data frames.
Loading dplyr package −
> library(dplyr)
Joining the two data frames −
> left_join(df1,df2, by = c("Class" = "Subject")) Class Age 1 Statistics NA 2 Maths 18 3 Chemistry 21 4 Physics 22 5 Economics 25 6 Political Science 21 7 Geography 23 Warning message: Column `Class`/`Subject` joining factors with different levels, coercing to character vector
It is showing a warning but it is not an issue because this warning is just telling us that there were different levels of factors in the two data frames.
Advertisements