Assignment 5

Author

Annalise Spaulding

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.

gap_1997 <- gapminder |>
  filter(year == 1997)
americas <- gap_1997 |> 
  filter(continent == "Americas") |> 
  slice_min(pop, n = 2)
europe <- gap_1997 |> 
  filter(continent == "Europe") |> 
  slice_min(pop, n = 2)
americas_wide <- americas |> 
  select(metric = country, population = pop, life_expectancy = lifeExp) |> 
  pivot_longer(cols = c(population, life_expectancy), names_to = "variable", values_to = "value") |> 
  pivot_wider(names_from = metric, values_from = value)

europe_wide <- europe |> 
  select(metric = country, population = pop, life_expectancy = lifeExp) |> 
  pivot_longer(cols = c(population, life_expectancy), names_to = "variable", values_to = "value") |> 
  pivot_wider(names_from = metric, values_from = value)
final_table <- americas_wide |> 
  bind_cols(europe_wide |> select(-variable))
final_table |>
  gt(rowname_col = "variable") |>
  tab_header(
    title = md("Iceland"),
    subtitle = md("Fewer People, limger lives")
  ) |>
  tab_spanner(label = "Americas", columns = names(americas_wide)[-1]) |> 
  tab_spanner(label = "Europe", columns = names(europe_wide)[-1]) |> 
  tab_footnote(
    footnote = md("[Skál!](https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/)"),
    locations = cells_title(groups = "subtitle")
  )
Iceland
Fewer People, limger lives1
Americas
Europe
Trinidad and Tobago Jamaica Iceland Montenegro
population 1138101.000 2531311.000 271192.00 692651.000
life_expectancy 69.465 72.262 78.95 75.445
1 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.