library(tidyverse)
library(gapminder)
library(gt)
library(gtExtras)Assignment 5
Go to the shared posit.cloud workspace for this class and open the assign05 project. Open the assign05.qmd file and complete the exercises.
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.
The Grading Rubric is available at the end of this document. Note: you will receive deductions for not using tidyverse syntax in this assignment. That includes the use of filter, mutate, and the up-to-date pipe operator |>.
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.
This is your work area. Add as many code cells as you need.
library(dplyr)
library(tidyr)
library(gapminder)
# Filter the gapminder dataset for the year 1997 and continents Americas and Europe
data_1997 <- gapminder %>%
filter(year == 1997 & continent %in% c("Americas", "Europe")) %>%
group_by(continent) %>%
arrange(pop) %>%
slice(1:2)
# Select the two least populated countries per continen
# Pivot the data wider to create separate columns for population and life expectancy # for each country
population_wide <- data_1997 %>%
select(continent, country, pop) %>%
pivot_wider(names_from = country, values_from = pop)
lifeExp_wide <- data_1997 %>%
select(continent, country, lifeExp) %>%
pivot_wider(names_from = country, values_from = lifeExp)
# Combine the two wide datasets using union
final_table <- population_wide %>% union(lifeExp_wide)
#puting continent row names to columns
data_1997 |> rownames_to_column(var = "Americas") |> gt()| Americas | country | year | lifeExp | pop | gdpPercap |
|---|---|---|---|---|---|
| Americas | |||||
| 1 | Trinidad and Tobago | 1997 | 69.465 | 1138101 | 8792.573 |
| 2 | Jamaica | 1997 | 72.262 | 2531311 | 7121.925 |
| Europe | |||||
| 3 | Iceland | 1997 | 78.950 | 271192 | 28061.100 |
| 4 | Montenegro | 1997 | 75.445 | 692651 | 6465.613 |
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. |