library(gapminder)
library(tidyverse)
<- gapminder |>
new_gapminder filter(year == 2007,
>= 70,
lifeExp <= 20000) gdpPercap
Class activity solutions
|>
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
<- gapminder |>
new_gapminder mutate(log_gdp_percap = log(gdpPercap))
|>
gapminder mutate(log_gdp_percap = log(gdpPercap)) |>
filter(year == 2007,
>= 70) |>
lifeExp 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,
>= 70) |>
lifeExp 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
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
- 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.