6.1 C
London
Saturday, December 21, 2024
HomeStatistics TutorialRHow to Do an Inner Join in R (With Examples)

How to Do an Inner Join in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

There are two common ways to perform an inner join in R:

Method 1: Use Base R

merge(df1, df2, by='column_to_join_on')

Method 2: Use dplyr

library(dplyr)

inner_join(df1, df2, by='column_to_join_on')

Both methods will produce the same result, but the dplyr method will tend to work faster on extremely large datasets.

The following examples show how to use each of these functions in practice with the following data frames:

#define first data frame
df1 = data.frame(team=c('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'),
                 points=c(18, 22, 19, 14, 14, 11, 20, 28))

df1

  team points
1    A     18
2    B     22
3    C     19
4    D     14
5    E     14
6    F     11
7    G     20
8    H     28

#define second data frame
df2 = data.frame(team=c('A', 'B', 'C', 'D', 'G', 'H'),
                 assists=c(4, 9, 14, 13, 10, 8))

df2

  team assists
1    A       4
2    B       9
3    C      14
4    D      13
5    G      10
6    H       8

Example 1: Inner Join Using Base R

We can use the merge() function in base R to perform an inner join, using the ‘team’ column as the column to join on:

#perform inner join using base R
df3 team')

#view result
df3

  team points assists
1    A     18       4
2    B     22       9
3    C     19      14
4    D     14      13
5    G     20      10
6    H     28       8

Notice that only the teams that were in both datasets are kept in the final dataset.

Example 2: Inner Join Using dplyr

We can use the inner_join() function from the dplyr package to perform an inner join, using the ‘team’ column as the column to join on:

library(dplyr)

#perform inner join using dplyr 
df3 team')

#view result
df3

  team points assists
1    A     18       4
2    B     22       9
3    C     19      14
4    D     14      13
5    G     20      10
6    H     28       8

Notice that this matches the result we obtained from using the merge() function in base R.

Additional Resources

The following tutorials explain how to perform other common operations in R:

How to Do a Left Join in R
How to Do a Right Join in R
How to Add a Column to Data Frame in R
How to Drop Columns from Data Frame in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories