0
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.
There are four primary types of joins:
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.
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.
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.
A full join will return all values of rows and columns from both tables whether they are matching or not.
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.
1 2 3 4
# 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)
x
and y
, are the name of the dataframes that need to be joined.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
.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 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.
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:
This example will perform all four types of joins using the above functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
# 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
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.
0