1. The full_join verb
In Chapter 2, you learned about left and right joins.
2. Left and right joins
You used them to join tables like the Batmobile and Batwing, and see which pieces were in one but not the other.
What if instead of keeping all the observations in the left or the right tables, you wanted to keep all observations in both tables, whether or not they matched to each other?
3. The full join
In this lesson: you'll learn another of dplyr's joining verbs: full join.
4. Joining and filtering
Let's start with the same joining and filtering that was applied in Chapter 2, where we filter for just the Batmobile and the Batwing sets. In the exercises, we'll help you create similar subsets of the LEGO pieces.
5. Batmobile vs. Batwing
Recall that this results in two tables with three variables.
6. Joining it all together
A left join would keep all the observations in batmobile, a right join would keep all the observations in batwing. A full join keeps all the observations in either. All the other arguments, like by and suffix, are the same.
7. Full join result
The result of full join has NAs in both, though in this preview we may not see them.
Notice, there are 440 observations, which is more than either of the tables had originally. When you do a full join, you're looking at all the pieces from the Batmobile and all the pieces from the Batwing, as if you opened up the two boxes and poured them out next to each other.
8. Replace NA: multiple variables
Just as you learned to do with right join in the last chapter, you can use replace na from the tidyr package to replace NAs in quantity batmobile and quantity batwing columns with zero. Notice that you can replace NAs in multiple variables by separating them with commas.
9. Let's practice!
In the exercises, you'll be using full joins along with other dplyr verbs to compare, not just two sets, like the Batmobile and the Batwing, but also two themes, specifically Batman and Star Wars. Let's get started!