DATA 110 Week 6 Homework

Author

Emilio D.

Week 6 Homework

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
setwd("/Users/emiliodifilippantonio/Desktop/DATA 110/DATA 110 Working Directory")
nations <- read_csv("nations.csv")
Rows: 5275 Columns: 10
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): iso2c, iso3c, country, region, income
dbl (5): year, gdp_percap, population, birth_rate, neonat_mortal_rate

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nations <- nations |> mutate(gdp = gdp_percap * population / 10^12)
nationsGrepl <- grepl("united", nations$country, ignore.case = TRUE)
nationsGrepl2 <- nations[nationsGrepl, ]
italy <- subset(nations, nations$country == "Italy")
nations2 <- rbind(nationsGrepl2, italy)
nationsGraph1 <- nations2 |>
  ggplot(aes(x = year, y = gdp, color = country)) +
  geom_point() +
  geom_line() +
  scale_color_brewer(palette = "Set1") +
  labs(x = "Year",
       y = "GDP in trillions",
       title = "GDP of 'United' countries (and Italy)")
nationsGraph1

nations3 <- nations |> group_by(region, year) %>% summarize(regional_gdp = sum(gdp, na.rm = TRUE))
`summarise()` has grouped output by 'region'. You can override using the
`.groups` argument.
nations3
# A tibble: 175 × 3
# Groups:   region [7]
   region               year regional_gdp
   <chr>               <dbl>        <dbl>
 1 East Asia & Pacific  1990         5.52
 2 East Asia & Pacific  1991         6.03
 3 East Asia & Pacific  1992         6.50
 4 East Asia & Pacific  1993         7.04
 5 East Asia & Pacific  1994         7.64
 6 East Asia & Pacific  1995         8.29
 7 East Asia & Pacific  1996         8.96
 8 East Asia & Pacific  1997         9.55
 9 East Asia & Pacific  1998         9.60
10 East Asia & Pacific  1999        10.1 
# ℹ 165 more rows
nationsGraph2 <- nations3 |>
  ggplot(aes(x = year, y = regional_gdp, fill = region)) +
  geom_area() +
  scale_fill_brewer(palette = "Set2") +
  labs(x = "Year",
       y = "GDP in trillions",
       title = "GDP in Trillions by World Bank Region")
nationsGraph2