How to use this file: Run the code chunks in order. Copy your output (numbers, tables, plots) into the HW2 Answer Worksheet (Word document). Write all answers there — not in this file.
Install the gapminder package the first time only — then
comment out the install.packages line.
# install.packages("gapminder") # run once if needed, then comment out
library(tidyverse)
library(gapminder)
The Gapminder dataset contains country-level development statistics compiled by the Gapminder Foundation. We use the 2007 cross-section — one row per country.
# Filter to 2007 only
gap07 <- gapminder %>% filter(year == 2007)
dim(gap07)
## [1] 142 6
glimpse(gap07)
## Rows: 142
## Columns: 6
## $ country <fct> "Afghanistan", "Albania", "Algeria", "Angola", "Argentina", …
## $ continent <fct> Asia, Europe, Africa, Africa, Americas, Oceania, Europe, Asi…
## $ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, …
## $ lifeExp <dbl> 43.828, 76.423, 72.301, 42.731, 75.320, 81.235, 79.829, 75.6…
## $ pop <int> 31889923, 3600523, 33333216, 12420476, 40301927, 20434176, 8…
## $ gdpPercap <dbl> 974.5803, 5937.0295, 6223.3675, 4797.2313, 12779.3796, 34435…
💡 Translation guide — use these in your writing, not the R names:
R name Plain English countrycountry name continentworld region lifeExplife expectancy at birth (years) poppopulation gdpPercapGDP per capita (USD)
dim(gap07)
## [1] 142 6
head(gap07,5)
## # A tibble: 5 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 2007 43.8 31889923 975.
## 2 Albania Europe 2007 76.4 3600523 5937.
## 3 Algeria Africa 2007 72.3 33333216 6223.
## 4 Angola Africa 2007 42.7 12420476 4797.
## 5 Argentina Americas 2007 75.3 40301927 12779.
summary(gap07)
## country continent year lifeExp
## Afghanistan: 1 Africa :52 Min. :2007 Min. :39.61
## Albania : 1 Americas:25 1st Qu.:2007 1st Qu.:57.16
## Algeria : 1 Asia :33 Median :2007 Median :71.94
## Angola : 1 Europe :30 Mean :2007 Mean :67.01
## Argentina : 1 Oceania : 2 3rd Qu.:2007 3rd Qu.:76.41
## Australia : 1 Max. :2007 Max. :82.60
## (Other) :136
## pop gdpPercap
## Min. :1.996e+05 Min. : 277.6
## 1st Qu.:4.508e+06 1st Qu.: 1624.8
## Median :1.052e+07 Median : 6124.4
## Mean :4.402e+07 Mean :11680.1
## 3rd Qu.:3.121e+07 3rd Qu.:18008.8
## Max. :1.319e+09 Max. :49357.2
##
sapply(gap07,class)
## country continent year lifeExp pop gdpPercap
## "factor" "factor" "integer" "numeric" "integer" "numeric"
Here is how to compute the mean and median for one variable:
gap07 %>%
summarise(
n = n(),
mean_lifeExp = mean(lifeExp, na.rm = TRUE),
median_lifeExp = median(lifeExp, na.rm = TRUE)
)
## # A tibble: 1 × 3
## n mean_lifeExp median_lifeExp
## <int> <dbl> <dbl>
## 1 142 67.0 71.9
YOUR TURN: Extend the code above to also compute the
mean and median for gdpPercap:
gap07 %>%
summarise(
mean_LifeExp = mean(lifeExp, na.rm = TRUE),
median_lifeExp = median(lifeExp, na.rm = TRUE),
mean_gdpPercap = mean(gdpPercap, na.rm = TRUE),
median_gdpPercap = median(gdpPercap, na.rm = TRUE),
)
## # A tibble: 1 × 4
## mean_LifeExp median_lifeExp mean_gdpPercap median_gdpPercap
## <dbl> <dbl> <dbl> <dbl>
## 1 67.0 71.9 11680. 6124.
Here is how to compute standard deviation, IQR, and range:
gap07 %>%
summarise(
sd_lifeExp = sd(lifeExp, na.rm = TRUE),
IQR_lifeExp = IQR(lifeExp, na.rm = TRUE),
min_lifeExp = min(lifeExp, na.rm = TRUE),
max_lifeExp = max(lifeExp, na.rm = TRUE)
)
## # A tibble: 1 × 4
## sd_lifeExp IQR_lifeExp min_lifeExp max_lifeExp
## <dbl> <dbl> <dbl> <dbl>
## 1 12.1 19.3 39.6 82.6
YOUR TURN: Build a complete summary table for
lifeExp and gdpPercap together. The life
expectancy section is started for you — add the same six statistics for
gdpPercap:
gapminder %>%
summarise(
mean_le = mean(lifeExp, na.rm = TRUE),
median_le = median(lifeExp, na.rm = TRUE),
sd_le = sd(lifeExp, na.rm = TRUE),
IQR_le = IQR(lifeExp, na.rm = TRUE),
min_le = min(lifeExp, na.rm = TRUE),
max_le = max(lifeExp, na.rm = TRUE),
max_gdp = mean(gdpPercap, na.rm = TRUE),
median_gdp = median(gdpPercap, na.rm = TRUE),
sd_gdp = sd(gdpPercap, na.rm = TRUE),
IQR_gdp = IQR(gdpPercap, na.rm = TRUE),
min_gdp = min(gdpPercap, na.rm = TRUE),
max_gdp = max(gdpPercap, na.rm = TRUE)
)
## # A tibble: 1 × 11
## mean_le median_le sd_le IQR_le min_le max_le max_gdp median_gdp sd_gdp IQR_gdp
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 59.5 60.7 12.9 22.6 23.6 82.6 113523. 3532. 9857. 8123.
## # ℹ 1 more variable: min_gdp <dbl>
Here is how to compute summary statistics separately by group:
gap07 %>%
group_by(continent) %>%
summarise(
n = n(),
mean_lifeExp = mean(lifeExp, na.rm = TRUE),
sd_lifeExp = sd(lifeExp, na.rm = TRUE)
)
## # A tibble: 5 × 4
## continent n mean_lifeExp sd_lifeExp
## <fct> <int> <dbl> <dbl>
## 1 Africa 52 54.8 9.63
## 2 Americas 25 73.6 4.44
## 3 Asia 33 70.7 7.96
## 4 Europe 30 77.6 2.98
## 5 Oceania 2 80.7 0.729
YOUR TURN: Extend the table above to also include
median_lifeExp and median_gdpPercap for each
continent:
gap07 %>%
group_by(continent) %>%
summarise(
n = n(),
mean_lifeExp = mean(lifeExp, na.rm = TRUE),
sd_lifeExp = sd(lifeExp, na.rm = TRUE),
median_lifeExp = median(lifeExp, na.rm = TRUE),
median_gdp = median(gdpPercap, na.rm = TRUE)
)
## # A tibble: 5 × 6
## continent n mean_lifeExp sd_lifeExp median_lifeExp median_gdp
## <fct> <int> <dbl> <dbl> <dbl> <dbl>
## 1 Africa 52 54.8 9.63 52.9 1452.
## 2 Americas 25 73.6 4.44 72.9 8948.
## 3 Asia 33 70.7 7.96 72.4 4471.
## 4 Europe 30 77.6 2.98 78.6 28054.
## 5 Oceania 2 80.7 0.729 80.7 29810.
Here is a histogram of life expectancy:
ggplot(gap07, aes(x = lifeExp)) +
geom_histogram(binwidth = 5, fill = "#BE398D", color = "white", alpha = 0.85) +
labs(
title = "Distribution of Life Expectancy Across Countries (2007)",
subtitle = "One bar = 5-year interval",
x = "Life expectancy at birth (years)",
y = "Number of countries"
) +
theme_minimal()
Make a histogram of gdpPercap, then a second histogram
of log(gdpPercap):
# Histogram 1: raw GDP per capita
ggplot(gap07, aes(x = gdpPercap)) +
geom_histogram(binwidth = 5000, fill = "#BF2C34", color = "white", alpha = 0.85) +
labs(
title = "Distribution of GDP per Capita Across Countries (2007)",
x = "GDP per capita (USD)",
y = "Number of countries"
) +
theme_minimal()
# Histogram 2: log-transformed GDP per capita
ggplot(gap07, aes(x = log(gdpPercap))) +
geom_histogram(binwidth = 0.5, fill = "#4F5900", color = "white", alpha = 0.85) +
labs(
title = "Distribution of Log GDP per Capita (2007)",
x = "Log GDP per capita",
y = "Number of countries"
) +
theme_minimal()
Here is a side-by-side boxplot comparing life expectancy across continents:
ggplot(gap07, aes(x = continent, y = lifeExp, fill = continent)) +
geom_boxplot(alpha = 0.7, outlier.color = "gray40") +
labs(
title = "Life Expectancy by World Region (2007)",
subtitle = "Each box summarizes 25th–75th percentile; line = median",
x = NULL,
y = "Life expectancy at birth (years)"
) +
theme_minimal() +
theme(legend.position = "none")
YOUR TURN: Adapt the example above to compare
gdpPercap across continents. Add
+ scale_y_log10() as a layer to handle the right skew:
ggplot(gap07, aes(x = continent, y = gdpPercap, fill = continent)) +
scale_y_log10()+
geom_boxplot(alpha = 0.7, outlier.color = "gray40") +
labs(
title = "GDP per Capita by World Region (2007)",
subtitle = "Each box summarizes 25th–75th percentile; line = median",
x = NULL,
y = "GDP per Capita (USD)"
) +
theme_minimal() +
theme(legend.position = "none")
Here is how to find the country with the highest and lowest value for a variable:
# Country with highest life expectancy
gap07[which.max(gap07$lifeExp), ]
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Japan Asia 2007 82.6 127467972 31656.
# Country with lowest life expectancy
gap07[which.min(gap07$lifeExp), ]
## # A tibble: 1 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Swaziland Africa 2007 39.6 1133066 4513.
YOUR TURN: Find the five countries with the highest
and five with the lowest GDP per capita. Use arrange() from
tidyverse:
gapminder %>%
filter(year == 2007) %>%
arrange(gdpPercap) %>%
head(5)
## # A tibble: 5 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Congo, Dem. Rep. Africa 2007 46.5 64606759 278.
## 2 Liberia Africa 2007 45.7 3193942 415.
## 3 Burundi Africa 2007 49.6 8390505 430.
## 4 Zimbabwe Africa 2007 43.5 12311143 470.
## 5 Guinea-Bissau Africa 2007 46.4 1472041 579.
gapminder %>%
filter(year == 2007) %>%
arrange (desc(gdpPercap)) %>%
head(5)
## # A tibble: 5 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Norway Europe 2007 80.2 4627926 49357.
## 2 Kuwait Asia 2007 77.6 2505559 47307.
## 3 Singapore Asia 2007 80.0 4553009 47143.
## 4 United States Americas 2007 78.2 301139947 42952.
## 5 Ireland Europe 2007 78.9 4109086 40676.