Assignment 5

Author

Zachary Howe

As best you can, recreate this table showing the population and life expectancy for the two least populated countries in the Americas and Europe for 1997. Your assignment must show all code in the output. Use as many code chunks as you feel necessary, I recommend first getting the output to match the output below. Hint: you will probably find it easiest to use two pivot_wider() functions with a union() to replicate the data layout. Use the gapminder dataset.

The Grading Rubric is available at the end of this document.

The word Skál! should link to https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/ it doesn’t in the image because it is an image. The following packages are useful and loaded for you.

library(tidyverse)
library(gapminder)
library(gt)
library(gtExtras)

This is your work area. Add as many code cells as you need.

# Load necessary libraries
library(tidyverse)
library(gapminder)
library(scales)

Attaching package: 'scales'
The following object is masked from 'package:purrr':

    discard
The following object is masked from 'package:readr':

    col_factor
# Step 1: Get 2 least populated countries per continent (Americas & Europe) for 1997
small_countries <- gapminder %>%
  filter(year == 1997, continent %in% c("Americas", "Europe")) %>%
  group_by(continent) %>%
  slice_min(pop, n = 2) %>%
  ungroup()

# Step 2: Format population row
pop_row <- small_countries %>%
  select(country, value = pop) %>%
  mutate(value = comma(value)) %>%
  pivot_wider(names_from = country, values_from = value) %>%
  mutate(variable = "population")

# Step 3: Format life expectancy row
lifeExp_row <- small_countries %>%
  select(country, value = lifeExp) %>%
  mutate(value = as.character(round(value, 1))) %>%
  pivot_wider(names_from = country, values_from = value) %>%
  mutate(variable = "life expectancy")

# Step 4: Combine both rows
final_table <- union(pop_row, lifeExp_row) %>%
  select(variable, everything())

# Print final table in console
print(final_table)
# A tibble: 2 × 5
  variable        `Trinidad and Tobago` Jamaica   Iceland Montenegro
  <chr>           <chr>                 <chr>     <chr>   <chr>     
1 population      1,138,101             2,531,311 271,192 692,651   
2 life expectancy 69.5                  72.3      79      75.4      
# Render table using gt
final_table %>%
  gt() %>%
  tab_header(
    title = "Population and Life Expectancy for the Two Least Populated Countries in the Americas and Europe (1997)"
  ) %>%
  fmt_number(
    columns = 2:3,
    decimals = 1
  ) %>%
  fmt_number(
    columns = 4:5,
    suffixing = "M"
  ) %>%
  tab_spanner(
    label = "Population",
    columns = 2:3
  ) %>%
  tab_spanner(
    label = "Life Expectancy",
    columns = 4:5
  ) %>%
  tab_source_note(
    source_note = "Source: Gapminder dataset"
  )
Population and Life Expectancy for the Two Least Populated Countries in the Americas and Europe (1997)
variable
Population
Life Expectancy
Trinidad and Tobago Jamaica Iceland Montenegro
population 1,138,101 2,531,311 271,192 692,651
life expectancy 69.5 72.3 79 75.4
Source: Gapminder dataset

Skál!

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Narrative: typos and grammatical errors
(7%)
Document formatting: correctly implemented instructions
(7%)
Accuracy of replicated image as a gt table
(78% )
Submitted properly to Brightspace
(8%)
NA NA You must submit according to instructions to receive any credit for this portion.