# 1 Learning Objectives

suppressPackageStartupMessages(library(dplyr))
suppressPackageStartupMessages(library(gapminder))

#### 1.0.0.1 Resources

Some useful resources for data frame joins.

• Jenny’s Cheatsheet for dplyr join functions.
• The Relational Data chapter in “R for Data Science” goes through these concepts in detail.

After going through the dplyr vignette on “two-table verbs”, we’ll work on the following exercises.

Consider the following areas of countries, in hectares:

(areas <- data.frame(country=c("Canada", "United States", "India", "Vatican City"),
area=c(998.5*10^6, 983.4*10^6, 328.7*10^6, 44)) %>%
as.tbl)
1. To the gapminder dataset, add an area variable using the areas tibble. Be sure to preserve all the rows of the original gapminder dataset.
left_join(gapminder, areas)
1. To the gapminder dataset, add an area variable using the areas tibble, but only keeping obervations for which areas are available.
inner_join(gapminder, areas)
1. Use a _join function to output the rows in areas corresponding to countries that are not found in the gapminder dataset.
anti_join(areas, gapminder)
1. Use a _join function to output the rows in areas corresponding to countries that are found in the gapminder dataset.
semi_join(areas, gapminder, by="country")
1. Construct a tibble that joins gapminder and areas, so that all rows found in each original tibble are also found in the final tibble.
full_join(areas, gapminder)
full_join(gapminder, areas)

Here are the rows containing the Vatican City:

full_join(areas, gapminder) %>%
filter(country=="Vatican City")
1. Subset the gapminder dataset to have countries that are only found in the areas data frame.
semi_join(gapminder, areas)
1. Subset the gapminder dataset to have countries that are not found in the areas data frame.
anti_join(gapminder, areas)

Let’s check…. Canada should not be in there:

anti_join(gapminder, areas) %>%
filter(country=="Canada")
LS0tCnRpdGxlOiAiU1RBVCA1NDUgQ2xhc3MgTWVldGluZyAwOSIKb3V0cHV0OgogICAgaHRtbF9ub3RlYm9vazoKICAgICAgICB0b2M6IHRydWUKICAgICAgICB0aGVtZTogY2VydWxlYW4KICAgICAgICBudW1iZXJfc2VjdGlvbnM6IHRydWUKZWRpdG9yX29wdGlvbnM6IAogIGNodW5rX291dHB1dF90eXBlOiBpbmxpbmUKLS0tCgojIExlYXJuaW5nIE9iamVjdGl2ZXMKCgpgYGB7cn0Kc3VwcHJlc3NQYWNrYW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