To merge two dataframes in R, you can use merge() function or *_join() function from dplyr package.
The following methods show how you can do it with syntax.
Method 1: Use merge() Function
merge(df1,df2,by="common_column")
Method 2: Use join() Function
library(dplyr)
*_join(df1, df2, by = "common_column")
Using join() function you can perform inner_join, right_join, left_join and outer_join operations on dataframe.
The following examples show how to merge two dataframes using merge and join function in R.
Using merge() Function
Let’s see how we can use merge() function in R:
# Create dataframe
df1 <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2001-01-19","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine3","Machine4","Machine5","Machine6"),
Value = c(108,120,135,95,98,105),Reading= c(110,97,91,89,80,85))
df2 <- data.frame( Machine_name = c("Machine1","Machine3","Machine4","Machine6"),
Value = c(120,121,107,105))
# Merge dataframe by inner join
df <- merge(df1,df2,by="Machine_name")
# Print dataframe
print(df)
Output:
Machine_name Start_date Value.x Reading Value.y
1 Machine1 2000-05-21 108 110 120
2 Machine3 2001-01-19 135 91 121
3 Machine4 2003-04-27 95 89 107
4 Machine6 2008-11-25 105 85 105
Here the output shows two dataframes are merge using merge() function based on common Machine_name column between them.
Using join() Function
Let’s see how we can use join() function in R.
Using inner_join() Function
Suppose we want to merge only those rows which are common between both dataframes then use inner_join() function:
library(dplyr)
# Create dataframe
df1 <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine4","Machine5","Machine6"),
Value = c(108,135,95,98,105),Reading= c(110,91,89,80,85))
df2 <- data.frame( Machine_name = c("Machine1","Machine3","Machine4","Machine6"),
Value = c(120,121,107,105))
# Merge dataframe by inner join
inner_df <- inner_join(df1, df2, by = "Machine_name")
# Show merge dataframe
print(inner_df)
Output:
Start_date Machine_name Value.x Reading Value.y
1 2000-05-21 Machine1 108 110 120
2 2003-04-27 Machine4 95 89 107
3 2008-11-25 Machine6 105 85 105
Here the output shows common rows between both dataframes.
Using left_join() Function
Let’s see how we can merge dataframe using left_join() function:
library(dplyr)
# Create dataframe
df1 <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine4","Machine5","Machine6"),
Value = c(108,135,95,98,105),Reading= c(110,91,89,80,85))
df2 <- data.frame( Machine_name = c("Machine1","Machine3","Machine4","Machine6"),
Value = c(120,121,107,105))
# Merge dataframe by left join
df <- left_join(df1,df2,by="Machine_name")
# Print dataframe
print(df)
Output:
Start_date Machine_name Value.x Reading Value.y
1 2000-05-21 Machine1 108 110 120
2 1998-03-28 Machine2 135 91 NA
3 2003-04-27 Machine4 95 89 107
4 2004-11-26 Machine5 98 80 NA
5 2008-11-25 Machine6 105 85 105
Here the output shows all rows from left dataframe and common rows from right dataframe.
Using right_join() Function
Let’s see how we can merge dataframe using right_join() function:
library(dplyr)
# Create dataframe
df1 <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine4","Machine5","Machine6"),
Value = c(108,135,95,98,105),Reading= c(110,91,89,80,85))
df2 <- data.frame( Machine_name = c("Machine1","Machine3","Machine4","Machine6"),
Value = c(120,121,107,105))
# Merge dataframe by right join
df <- right_join(df1,df2,by="Machine_name")
# Print dataframe
print(df)
Output:
Start_date Machine_name Value.x Reading Value.y
1 2000-05-21 Machine1 108 110 120
2 2003-04-27 Machine4 95 89 107
3 2008-11-25 Machine6 105 85 105
4 <NA> Machine3 NA NA 121
Here the output shows all rows from right dataframe and common rows from left dataframe based on Machine_name column.
Using full_join() Function
Let’s see how to common all rows of both dataframes:
library(dplyr)
# Create dataframe
df1 <- data.frame(Start_date=as.Date(c("2000-05-21","1998-03-28","2003-04-27","2004-11-26","2008-11-25")),
Machine_name = c("Machine1","Machine2","Machine4","Machine5","Machine6"),
Value = c(108,135,95,98,105),Reading= c(110,91,89,80,85))
df2 <- data.frame( Machine_name = c("Machine1","Machine3","Machine4","Machine6"),
Value = c(120,121,107,105))
# Merge dataframe by full join
df <- full_join(df1,df2,by="Machine_name")
# Print dataframe
print(df)
Output:
Start_date Machine_name Value.x Reading Value.y
1 2000-05-21 Machine1 108 110 120
2 1998-03-28 Machine2 135 91 NA
3 2003-04-27 Machine4 95 89 107
4 2004-11-26 Machine5 98 80 NA
5 2008-11-25 Machine6 105 85 105
6 <NA> Machine3 NA NA 121
Here the output shows all rows from both dataframes.