Assignment 5

Author

Colby Chavarie

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.

# Filter the dataset for the year 1997 
data_1997 <- gapminder %>%
  filter(year == 1997, continent %in% c("Americas", "Europe"))
# Two least populated countries per continent
least_populated <- data_1997 %>%
  group_by(continent) %>%
  arrange(pop) %>%
  slice_head(n = 2) %>%
  ungroup()
# Reshape Data
population_wide <- least_populated %>%
  select(country, continent, pop) %>%
  pivot_wider(names_from = continent, values_from = pop, names_glue = "{continent} Population")

life_expectancy_wide <- least_populated %>%
  select(country, continent, lifeExp) %>%
  pivot_wider(names_from = continent, values_from = lifeExp, names_glue = "{continent} Life Expectancy")
# Merge the Two Datasets
final_table <- population_wide %>%
  full_join(life_expectancy_wide, by = "country")
# Create a gt table
final_table %>%
  gt() %>%
  tab_header(title = md("Population and Life Expectancy in 1997")) %>%
  fmt_number(columns = contains("Population"), decimals = 0, use_seps = TRUE) %>%
  fmt_number(columns = contains("Life Expectancy"), decimals = 1) %>%
  tab_source_note(md("[Skál!](https://www.islandshotel.is/explore-iceland/blog/icelandic-phrases/)") )
Population and Life Expectancy in 1997
country Americas Population Europe Population Americas Life Expectancy Europe Life Expectancy
Trinidad and Tobago 1,138,101 NA 69.5 NA
Jamaica 2,531,311 NA 72.3 NA
Iceland NA 271,192 NA 79.0
Montenegro NA 692,651 NA 75.4
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.