How to Do an Outer Join in R

Spread the love

R provides numerous tools and libraries to manage, analyze, and manipulate data. One common operation when working with multiple data frames is joining them together, and in R, several join operations can be performed such as inner join, left join, right join, and outer join. In this article, we’ll discuss how to perform an outer join in R, which combines rows from two or more tables based on a related column, and if there is no match, the missing side will contain NULLs.

1. Introduction to Outer Joins

In relational database terminology, an outer join returns all the rows when there is a match in either the left or the right data frame. If there is no match, the missing side will contain NA.

There are two types of outer joins:

  • Full Outer Join: Returns all rows from both tables and places NA in the columns from the table that doesn’t have a match.
  • Partial Outer Join: Including Left and Right outer joins, returns all rows from one table and the matched rows from the other table, placing NA in the columns from the table that doesn’t have a match.

2. Basic Syntax

The basic syntax for performing an outer join in R using dplyr is as follows:

full_join(x, y, by = "ID")

Here, x and y are the data frames to be joined, and by represents the common column that both data frames share.

3. Example Data Frames

Let’s create two example data frames, employees and departments, which we will join using an outer join.

# Define the employees data frame
employees <- data.frame(
  emp_id = c(1, 2, 3, 4),
  emp_name = c("John", "Mike", "Sara", "Anna"),
  dept_id = c(1, 2, 3, NA)

# Define the departments data frame
departments <- data.frame(
  dept_id = c(1, 2, 3, 4),
  dept_name = c("HR", "IT", "Finance", "Marketing")


  emp_id emp_name dept_id
1      1     John       1
2      2     Mike       2
3      3     Sara       3
4      4     Anna      NA


  dept_id dept_name
1       1        HR
2       2        IT
3       3   Finance
4       4 Marketing

4. Performing Full Outer Join

A Full Outer Join can be executed with the full_join function from the dplyr package:

result <- full_join(employees, departments, by = "dept_id")


  emp_id emp_name dept_id dept_name
1      1     John       1        HR
2      2     Mike       2        IT
3      3     Sara       3   Finance
4      4     Anna      NA      <NA>
5     NA     <NA>       4 Marketing

This will return a new data frame result that contains all rows from both employees and departments with NA in the columns where there is no match.

5. Managing Duplicates

When performing outer joins, especially full outer joins, it’s crucial to manage duplicate rows effectively. The dplyr package offers functionality to deal with duplicates. If the joining columns have duplicate rows, the resultant data frame will have all possible combinations of those rows.

For instance, if we have a data frame:

df1 <- data.frame(
  ID = c(1, 1, 2),
  Value1 = c("a", "b", "c")

# another dataframe
df2 <- data.frame(
  ID = c(1, 2, 2),
  Value2 = c("x", "y", "z")

A full outer join on these data frames by the “ID” column would produce:

result <- full_join(df1, df2, by = "ID")


  ID Value1 Value2
1  1      a      x
2  1      b      x
3  2      c      y
4  2      c      z

The resultant data frame will have rows containing all combinations of rows from df1 and df2 where the ID is matching.

6. Handling Non-Matching Keys

One of the advantages of using outer joins is the ability to retain non-matching keys. This means that even if certain keys (values in the joining column) are not present in one of the data frames, the resultant data frame will still contain those keys, with NA in the columns of the data frame that doesn’t have those keys. This is especially useful when you do not want to lose any information from either of the data frames being joined.

7. Additional Considerations

When performing outer joins, the type and class of the joining columns should ideally be the same in both data frames. If they are different, it might lead to unexpected results or errors. It is recommended to explicitly check and convert the types of joining columns before performing the join if needed.

8. Outer Joins in Base R

While dplyr is a powerful and user-friendly package for performing joins, it is also possible to perform outer joins using base R functionality, using the merge function:

result <- merge(x = employees, y = departments, by = "dept_id", all = TRUE)


  dept_id emp_id emp_name dept_name
1       1      1     John        HR
2       2      2     Mike        IT
3       3      3     Sara   Finance
4       4     NA     <NA> Marketing
5      NA      4     Anna      <NA>

The all parameter set to TRUE specifies that a full outer join should be performed.

9. Conclusion

In R, outer joins, including full outer joins, are pivotal in combining different datasets effectively without losing information. The dplyr package, a member of the tidyverse family, offers a streamlined and intuitive syntax for performing such joins, aiding data scientists and statisticians in handling and manipulating data efficiently.

When performing outer joins, careful consideration is required to manage duplicates, handle non-matching keys, and ensure the compatibility of joining columns. The resultant data frame of an outer join encompasses all the information from the input data frames, filled with NA where the data is missing, allowing for comprehensive analysis and further data manipulation.

Posted in RTagged

Leave a Reply