Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Merging DataFrames in R

Joining datasets helps give a clear view of the relationship between data stored in different tables or sources.

Oct 27, 2020 • 6 Minute Read

Introduction

Merging or joining data frames is the process of combining columns from two or more dataframes. It is a well-known operation in programming. In R we can perform join with two functions: merge() of the base package and join() of a dplyr package. Before getting into that, this guide will go through the types of joins.

Types of Joins

There are four primary types of joins:

Left Outer Join

Suppose you are joining two tables, A and B, where A is the left table and B is the right table. When you perform a left outer join on A and B, it will return all rows from A and rows that are matching in B. All columns from A and B are returned, but the rows that do not match in B will have NA values for B columns.

Right Outer Join

A right outer join works similarly to the left outer join. It will return all matching rows from the right table in the left table. All columns from both tables are returned, and the rows that do not match in the left table will have NA values.

Inner Join

An inner join will return all the matching rows from both tables. If there are multiple matches between both tables, all combinations will be returned.

Full Join

A full join will return all values of rows and columns from both tables whether they are matching or not.

Joining Dataframes with merge()

The merge() function belongs to the base package of R. You don't need to install any additional packages to use the merge() function. The arguments of the merge() function, along with the default values that are passed in those arguments, are given below.

      # Syntax for merge function
merge(x, y, by = intersect(names(x), names(y)),
      by.x = by, by.y = by, all = FALSE, all.x = all, all.y = all)
    
  • The first two arguments, x and y, are the name of the dataframes that need to be joined.
  • The next three arguments, by, by.x, and by.y, decide the column used for joining the dataframes. If the name of the column that is needed for joining is the same then you don't need to pass any names. If they are different then you have to pass the names in by.x and by.y.
  • The next three arguments decide the type of join performed by the merge(). The default values will perform an inner join. If all.x is set to TRUE then it will perform a left outer join. If all.y is set to TRUE then it will perform a right outer join. If both are set to TRUE then it will perform a full outer join.

The dataframes used in this example are band_members and band_instruments. The column details are shared below.

      # Loading dplyr function to use the datasets present in the package
library(dplyr)
data(band_members)
data(band_instruments)

# Columns in band_instruments
colnames(band_instruments)
[1] "name"  "plays"

#Columns in band_members
colnames(band_members)
[1] "name" "band"

# Lets look at the data
view(band_instruments)
  name  plays 
  <chr> <chr> 
1 John  guitar
2 Paul  bass  
3 Keith guitar

view(band_members)
  name  band   
  <chr> <chr>  
1 Mick  Stones 
2 John  Beatles
3 Paul  Beatles
    

The code in the next example will perform all four types of joins using the dataframes above and the merge() function.

      # Performing Left outer join
merge(band_members, band_instruments,  all.x = TRUE)
  name    band  plays
1 John Beatles guitar
2 Mick  Stones   <NA>
3 Paul Beatles   bass

# Performing Right outer join
merge(band_members, band_instruments,  all.y = TRUE)
   name    band  plays
1  John Beatles guitar
2 Keith    <NA> guitar
3  Paul Beatles   bass

# Performing Inner join
merge(band_members, band_instruments,  all.y = TRUE, all.x = TRUE)
   name    band  plays
1  John Beatles guitar
2 Keith    <NA> guitar
3  Mick  Stones   <NA>
4  Paul Beatles   bass

# Performing Full outer join
merge(band_members, band_instruments)
  name    band  plays
1 John Beatles guitar
2 Paul Beatles   bass
    

In the output of joins you can see that if the matching values are not there they are assigned as <NA>. In the case of an inner join, it is only showing the matching values from both dataframes.

Joining Dataframes with dplyr:: join Function

In comparison to the merge() function, dplyr has four different functions for different types of joins. It avoids confusion because you don't have to set values of the arguments. The join functions are given below:

  • inner_join()
  • left_join()
  • right_join()
  • full_join()

This example will perform all four types of joins using the above functions.

      # Performing Inner join
inner_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass

# Performing Left outer join  
left_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  NA    
2 John  Beatles guitar
3 Paul  Beatles bass 

# Performing Right outer join 
 right_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 John  Beatles guitar
2 Paul  Beatles bass  
3 Keith NA      guitar

# Performing Full outer join
full_join(band_members, band_instruments, by = "name")

  name  band    plays 
  <chr> <chr>   <chr> 
1 Mick  Stones  NA    
2 John  Beatles guitar
3 Paul  Beatles bass  
4 Keith NA      guitar
    

Conclusion

When we start working with data stored in different tables or sources then we will start exploring the relationship between them. In this process, we join datasets to get a clear view. This operation happens in every project that works around data.

Nishant Kumar Singh

Nishant K.

Nishant is an assessment and written content author.

More about this author