artists# A tibble: 3 × 2
name followers
<chr> <dbl>
1 Beyoncé 24757958
2 Taylor Swift 33098116
3 Ariana Grande 51807131
Reading, Joining, and Pivoting
left_join(x, y)right_join(x, y)full_join(x, y)inner_join(x, y) and semi_join(x, y)anti_join(x, y)something_join(x, y)| Function | x | y | ||
|---|---|---|---|---|
| Type | rows | columns | rows | columns |
left_join()
|
all | all | matched | all |
right_join()
|
matched | all | all | all |
full_join()
|
all | all | all | all |
inner_join()
|
matched | all | matched | all |
semi_join()
|
matched | all | none | none |
anti_join()
|
unmatched | all | none | none |
# A tibble: 5 × 4
name song_name album_name song_popularity
<chr> <chr> <chr> <dbl>
1 Beyoncé Savage Remix (feat. Beyoncé) Savage Rem… 83
2 Taylor Swift cardigan folklore 85
3 Drake Laugh Now Cry Later (feat. Lil Durk) Laugh Now … 95
4 Beyoncé Halo I AM…SASHA… NA
5 Ariana Grande Stuck with U (with Justin Bieber) Stuck with… NA
left_join()# A tibble: 5 × 5
name song_name album_name song_popularity followers
<chr> <chr> <chr> <dbl> <dbl>
1 Beyoncé Savage Remix (feat. Beyonc… Savage Re… 83 24757958
2 Taylor Swift cardigan folklore 85 33098116
3 Drake Laugh Now Cry Later (feat.… Laugh Now… 95 NA
4 Beyoncé Halo I AM…SASH… NA 24757958
5 Ariana Grande Stuck with U (with Justin … Stuck wit… NA 51807131
right_join()# A tibble: 4 × 5
name song_name album_name song_popularity followers
<chr> <chr> <chr> <dbl> <dbl>
1 Beyoncé Savage Remix (feat. Beyonc… Savage Re… 83 24757958
2 Taylor Swift cardigan folklore 85 33098116
3 Beyoncé Halo I AM…SASH… NA 24757958
4 Ariana Grande Stuck with U (with Justin … Stuck wit… NA 51807131
full_join()# A tibble: 5 × 5
name song_name album_name song_popularity followers
<chr> <chr> <chr> <dbl> <dbl>
1 Beyoncé Savage Remix (feat. Beyonc… Savage Re… 83 24757958
2 Taylor Swift cardigan folklore 85 33098116
3 Drake Laugh Now Cry Later (feat.… Laugh Now… 95 NA
4 Beyoncé Halo I AM…SASH… NA 24757958
5 Ariana Grande Stuck with U (with Justin … Stuck wit… NA 51807131
full_join() x2# A tibble: 5 × 6
name song_name album_name song_popularity followers album_release_date
<chr> <chr> <chr> <dbl> <dbl> <date>
1 Beyoncé Savage R… Savage Re… 83 24757958 2020-04-29
2 Taylor Swift cardigan folklore 85 33098116 NA
3 Drake Laugh No… Laugh Now… 95 NA 2020-08-14
4 Beyoncé Halo I AM…SASH… NA 24757958 2008-11-14
5 Ariana Gran… Stuck wi… Stuck wit… NA 51807131 2020-05-08
Importing data will depend on where the dataset is on your computer. However we use the help of here::here() function. This function sets the working directory to the project folder (i.e. where the .Rproj file is).
In tidyverse (specifically {tidyr}), pivoting refers to reshaping a data frame so that it is either:
longer: more rows, fewer columns, or
wider: fewer rows, more columns.
gapminder is in long format: one row per country-year
# A tibble: 1,704 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
7 Afghanistan Asia 1982 39.9 12881816 978.
8 Afghanistan Asia 1987 40.8 13867957 852.
9 Afghanistan Asia 1992 41.7 16317921 649.
10 Afghanistan Asia 1997 41.8 22227415 635.
# ℹ 1,694 more rows
pivot_wider(): Long → Wide# A tibble: 142 × 13
country `1952` `1957` `1962` `1967` `1972` `1977` `1982` `1987` `1992` `1997`
<fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Afghan… 28.8 30.3 32.0 34.0 36.1 38.4 39.9 40.8 41.7 41.8
2 Albania 55.2 59.3 64.8 66.2 67.7 68.9 70.4 72 71.6 73.0
3 Algeria 43.1 45.7 48.3 51.4 54.5 58.0 61.4 65.8 67.7 69.2
4 Angola 30.0 32.0 34 36.0 37.9 39.5 39.9 39.9 40.6 41.0
5 Argent… 62.5 64.4 65.1 65.6 67.1 68.5 69.9 70.8 71.9 73.3
6 Austra… 69.1 70.3 70.9 71.1 71.9 73.5 74.7 76.3 77.6 78.8
7 Austria 66.8 67.5 69.5 70.1 70.6 72.2 73.2 74.9 76.0 77.5
8 Bahrain 50.9 53.8 56.9 59.9 63.3 65.6 69.1 70.8 72.6 73.9
9 Bangla… 37.5 39.3 41.2 43.5 45.3 46.9 50.0 52.8 56.0 59.4
10 Belgium 68 69.2 70.2 70.9 71.4 72.8 73.9 75.4 76.5 77.5
# ℹ 132 more rows
# ℹ 2 more variables: `2002` <dbl>, `2007` <dbl>
pivot_wider(): What Happened?year becomes its own columnlifeExp values get spread into those columnscountry remainsGreat for making tables or comparing across time side by side.
pivot_longer(): Wide → LongNow reverse it — take life_wide back to long format:
# A tibble: 1,704 × 3
country year lifeExp
<fct> <chr> <dbl>
1 Afghanistan 1952 28.8
2 Afghanistan 1957 30.3
3 Afghanistan 1962 32.0
4 Afghanistan 1967 34.0
5 Afghanistan 1972 36.1
6 Afghanistan 1977 38.4
7 Afghanistan 1982 39.9
8 Afghanistan 1987 40.8
9 Afghanistan 1992 41.7
10 Afghanistan 1997 41.8
# ℹ 1,694 more rows
pivot_longer(): What Happened?year and lifeExpcountry is repeated across rows# A tibble: 12 × 3
year Australia `New Zealand`
<int> <dbl> <dbl>
1 1952 10040. 10557.
2 1957 10950. 12247.
3 1962 12217. 13176.
4 1967 14526. 14464.
5 1972 16789. 16046.
6 1977 18334. 16234.
7 1982 19477. 17632.
8 1987 21889. 19007.
9 1992 23425. 18363.
10 1997 26998. 21050.
11 2002 30688. 23190.
12 2007 34435. 25185.
The {ggplot2} package has many “extensions” by the tidyverse team and others in the community.
One of these packages is {gganimate} which shows an excellent use of the gapminder data.
If you like thinking about data and public health, you might like Hans Rosling’s talk The best stats you’ve ever seen. You can also take the Gapminder Test.