Get startedGet started for free

The right_join verb

1. The right_join verb

In the last lesson, you learned about the left join verb.

2. The right join

You used it to examine all the pieces in the Batmobile LEGO set and see which appeared and which were missing in the Batwing LEGO set.

3. The left and right join

Once you've seen a left join, it might not surprise you to learn that there's also a right join. Just as left joins keep all the observations from the first (or "left") table, whether or not they appear in the second (or "right") table, a right join keeps all the observations in the second (or "right") table, whether or not they appear in the first table.

4. Mirror images

Notice that with a right join of batmobile on batwing, we see NAs in the quantity batmobile column, but not quantity batwing. This shows us that left joins and right joins are mirror images of each other. Besides comparing the Batmobile and Batwing LEGO sets, let's consider another case where we'd use right join in practice, to determine how often each theme appears among the sets in our database.

5. Count and sort

You can use dplyr's count verb to calculate the number of sets that has each theme id, along with sort equals TRUE to arrange them. Knowing the most common is theme id 501 is not very helpful, so let's add more information, like the name of each theme.

6. Inner join

You could use an inner join for this. That tells us that the most common theme is called "Gear", and the second most common is called "Friends". But this is missing something: specifically, any themes that never occurred in any set in this database. That wouldn't have a zero in this data: it just would not appear. But a right join would keep those themes that never occurred.

7. Right join

Notice this has NAs in the n column for anything that wasn't in the count table- for instance, the Farm theme, in the eighth observation. Now, we know from our data that those NAs in n should really be zeros.

8. Replace NAs

So let's introduce a new verb from the tidyr package: replace na. By piping your result to replace underscore na, list parentheses n equals zero, we can replace any NA in the n column with zero. Notice that the Farm observation now has a zero rather than an NA. The replace na verb often appears when you're doing joins: you'll gain practice with it in the exercises and in the rest of the course.

9. Let's practice!

So, let's practice!