Data Frame Operations - Joining/Merging Two Data Frames With Different Column Name

Introduction

 
So, far in one of my previous article, we have learned what is the join, its benefit and then what are the different types of joins available in R to join two data frames in R. If you have missed that article then you learn that from the article link Merging/Joining Two Data Frames In R
 
Now, in this blog, we shall learn to join the data frames when the column name in both data frame is different.
Let's start now.
 

Joining Two Data Frames By Different Column Name

 
In general, to join two data frames in R, we use the below sentence when the column name in both the data frames are the same based on which we want to join both the data frames. 
  1. #R Data Frame Natural join arguments    
  2. merge(x = <obj1>, y= <obj2>, by = "<Column Name>", all = <FALSE/TRUE>)  
Here as you can see clearly from the above syntax, we have not passed individual data frame column name in the argument by. It is because in both the data frames the column on which we want to merge the data frames are the same.
 
As said above the case is not the same always. Hence, sometimes we need to join the data frames even when the column name is different. Here the column name means the key which refers to the column on which we want to merge the data frames. In that case, we use the following syntax. 
  1. #Joining Both Data Frames - Inner Join  
  2. merge(x = <obj1>, y = <obj2>, by.x = "<column name>", by.y = "<column name>", all = <FALSE/TRUE>)  
The below example distinguishes the above concept in R. For this we will create two data frames and join them as shown below.
 
Data Frame Operations - Joining/Merging Two Data Frames With Different Column Name
 
To create the first data frame run the below R script.
  1. #Creating Employee Data Frame  
  2. dfEmployee  <- data.frame(EmpId = c(123),  
  3.                 Name = c("Josephine J. Evans""Sophie D. Sessions""Geneva R. Miller"))  
  4.   
  5. #Printing Employee Data Frame  
  6. dfEmployee  
To create the second data frame run the below R script code.
  1. #Creating Employee Address Data Frame  
  2. dfEmpAddress <- data.frame(EmployeeID = c(324),  
  3.                 Company = c("Monmax""Heilig-Meyers""MegaSolutions"))  
  4.   
  5. #Printing above data frame  
  6. dfEmpAddress  
Now, we have successfully created both the data frames and will join both using the column name, EmpId & EmployeeID which are the columns of first and second data frame respectively.
 

Merging Both Data Frames

 
To merge both the data frames we will run the below R script. Here I have used the natural join to know more about other types of R data frame joins you can follow the above article link. We use comma (,) to pass both column name as key for merging the data frames.
  1. #Joining Both Data Frames - Inner Join  
  2. merge(x = dfEmployee, y = dfEmpAddress,   
  3.       by.x = "EmpId", by.y = "EmployeeID", all = FALSE)  
Now we have already joined both the data frame with different column names i.e., EmpId and EmployeeId. It will give the output something like as shown in the image below.
 
Data Frame Operations - Joining/Merging Two Data Frames With Different Column Name
 

Summary

 
In this blog, we have learned how to join the two data frames in R when the column names are different. Apart from this we also have just recalled creating the data frame in the form of example practically.
 
I hope you learned and enjoyed it. I look forward to seeing your feedback.