8.3 Primer to joins
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# use double colon to specify which library you are getting a function from
# base::union()
table4a_tidy
## # A tibble: 6 x 3
## country year cases
## <chr> <chr> <int>
## 1 Afghanistan 2000 2666
## 2 Brazil 2000 80488
## 3 China 2000 213766
## 4 Afghanistan 1999 745
## 5 Brazil 1999 37737
## 6 China 1999 212258
table4b_tidy
## # A tibble: 6 x 3
## country year population
## <chr> <chr> <int>
## 1 Afghanistan 1999 19987071
## 2 Brazil 1999 172006362
## 3 China 1999 1272915272
## 4 Afghanistan 2000 20595360
## 5 Brazil 2000 174504898
## 6 China 2000 1280428583
left_join(x = table4a_tidy, y = table4b_tidy)
## Joining, by = c("country", "year")
## # A tibble: 6 x 4
## country year cases population
## <chr> <chr> <int> <int>
## 1 Afghanistan 2000 2666 20595360
## 2 Brazil 2000 80488 174504898
## 3 China 2000 213766 1280428583
## 4 Afghanistan 1999 745 19987071
## 5 Brazil 1999 37737 172006362
## 6 China 1999 212258 1272915272