Class activity solutions

Author

Ciaran Evans

library(gapminder)
library(tidyverse)

new_gapminder <- gapminder |>
  filter(year == 2007, 
         lifeExp >= 70, 
         gdpPercap <= 20000)
gapminder |>
  filter(year == 2007) |>
  count(continent)
# A tibble: 5 × 2
  continent     n
  <fct>     <int>
1 Africa       52
2 Americas     25
3 Asia         33
4 Europe       30
5 Oceania       2
new_gapminder <- gapminder |>
  mutate(log_gdp_percap = log(gdpPercap))
gapminder |>
  mutate(log_gdp_percap = log(gdpPercap)) |>
  filter(year == 2007,
         lifeExp >= 70) |>
  summarize(med_log_gdp = median(log_gdp_percap))
# A tibble: 1 × 1
  med_log_gdp
        <dbl>
1        9.40
gapminder |>
  mutate(log_gdp_percap = log(gdpPercap)) |>
  filter(year == 2007,
         lifeExp >= 70) |>
  group_by(continent) |>
  summarize(med_log_gdp = median(log_gdp_percap))
# A tibble: 5 × 2
  continent med_log_gdp
  <fct>           <dbl>
1 Africa           8.87
2 Americas         9.11
3 Asia             9.39
4 Europe          10.2 
5 Oceania         10.3 
  1. It does not matter in questin 5 whether we filter or mutate first.

gapminder |>
  filter(year == 2007) |>
  group_by(continent) |>
  summarize(med_life = median(lifeExp),
            correlation = cor(lifeExp, log(gdpPercap)),
            N = n())
# A tibble: 5 × 4
  continent med_life correlation     N
  <fct>        <dbl>       <dbl> <int>
1 Africa        52.9       0.452    52
2 Americas      72.9       0.780    25
3 Asia          72.4       0.800    33
4 Europe        78.6       0.836    30
5 Oceania       80.7       1         2
  1. Correlation between life expectancy and log GDP per capita is positive for all continents, and is stronger for the Americas, Asia, and Europe. There are only two countries in Oceania, so correlation is a meaningless statistic for Oceania.