“How can I perform a right join in R, and what are some examples of using it?”

“How can I perform a right join in R, and what are some examples of using it?”

A right join in R is a data manipulation technique that combines two data sets based on common values in a specified column, while also including all rows from the right data set. This means that all the rows from the right data set will be included in the final result, even if there are no matching values in the specified column. This is useful for merging data sets with missing values or when the focus is on the data in the right set. Some examples of using a right join in R include combining customer and sales data to identify customers who have not made purchases, merging job listings with applicant data to see which jobs have no applicants, and merging employee data with salary data to identify employees without salaries.

Do a Right Join in R (With Examples)


There are two common ways to perform a right join in R:

Method 1: Use Base R

merge(df1, df2, by='column_to_join_on', all.y=TRUE)

Method 2: Use dplyr

library(dplyr)

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

Both methods will return all rows from df2 and any rows with matching keys from df1.

It’s also worth noting that 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', 'L', 'M'),
                 assists=c(4, 9, 14, 13, 10, 8))

df2

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

Example 1: Right Join Using Base R

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

#perform right join using base R
df3 <- merge(df1, df2, by='team', all.y=TRUE)

#view result
df3

  team points assists
1    A     18       4
2    B     22       9
3    C     19      14
4    D     14      13
5    L     NA      10
6    M     NA       8

Notice that all of the rows from df2 were included in the final data frame, but only the rows from df1 that had a matching team name were included in the final data frame.

Example 2: Right Join Using dplyr

We can use the right_join() function from the package to perform a right join, using the ‘team’ column as the column to join on:

library(dplyr)

#perform right join using dplyr 
df3 <- right_join(df1, df2, by='team')

#view result
df3

  team points assists
1    A     18       4
2    B     22       9
3    C     19      14
4    D     14      13
5    L     NA      10
6    M     NA       8

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

Additional Resources

Cite this article

stats writer (2024). “How can I perform a right join in R, and what are some examples of using it?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-perform-a-right-join-in-r-and-what-are-some-examples-of-using-it/

stats writer. "“How can I perform a right join in R, and what are some examples of using it?”." PSYCHOLOGICAL SCALES, 29 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-perform-a-right-join-in-r-and-what-are-some-examples-of-using-it/.

stats writer. "“How can I perform a right join in R, and what are some examples of using it?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-perform-a-right-join-in-r-and-what-are-some-examples-of-using-it/.

stats writer (2024) '“How can I perform a right join in R, and what are some examples of using it?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-perform-a-right-join-in-r-and-what-are-some-examples-of-using-it/.

[1] stats writer, "“How can I perform a right join in R, and what are some examples of using it?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. “How can I perform a right join in R, and what are some examples of using it?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top