A little too excepting
1. A little too excepting
Just as anti join can be thought as the opposite of inner join, intersect also has a complementary function called setdiff. It gives the data that is in the first tibble, but not in the second tibble. Let's check that out first as a diagram before we head back to Uruguay for an example.2. setdiff diagram
setdiff returns all the values that are in left_col, except those that are also in right_col. Since the id values of 2 and 3 do not appear in the right_col data, setdiff returns those values when called with left_col as the first argument and right_col as the second.3. The tibbles again
Previously, when discussing union and union_all, we were looking to identify the rows in either tibble. Now we want to focus on only those rows that are in uruguay_imf_filtered but not in uruguay_wb_filtered. Can you spot them?4. setdiff()
The World Bank data does not have years prior to 2013 available, whereas the IMF data does. The arguments to setdiff are the same as we have seen throughout this chapter. The first argument is the Uruguay IMF data, and the second argument is the Uruguay World Bank data. setdiff will return the rows in the Uruguay IMF data, except for those that also appear in the Uruguay World Bank data. As 2010, 2011, and 2012 are the only three years in uruguay_imf_filtered and not in uruguay_wb_filtered; these are the three rows returned.5. Previous unions()
Another useful function that can check for whether two datasets are the same is setequal. It looks to see if two datasets contain the same rows, returning TRUE if they do and FALSE if they don't. This works even if the rows are in a slightly different order. Recall our two union calls from the last lesson, now called union_one_way and union_other. The same rows appear in both unions but in a slightly different order.6. setequal() example
Running setequal on these two unions gives us the expected result. TRUE is returned since the same rows are in both datasets.7. Let's practice!
Try out the exercises in this last lesson of Chapter 3!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.