Homework Data Tidying

Author

Adam Savino

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.2
Warning: package 'ggplot2' was built under R version 4.3.2
Warning: package 'dplyr' was built under R version 4.3.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.3     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.2     ✔ 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
library(janitor)

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(countrycode)
Warning: package 'countrycode' was built under R version 4.3.2
imf_climate_dashboards_green_debt_url <- "https://opendata.arcgis.com/datasets/8e2772e0b65f4e33a80183ce9583d062_0.csv"

green_debt <- imf_climate_dashboards_green_debt_url |> 
  read_csv() 
Rows: 355 Columns: 42
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (12): Country, ISO2, ISO3, Indicator, Unit, Source, CTS_Code, CTS_Name, ...
dbl (30): ObjectId, F1985, F1986, F1987, F1990, F1991, F1992, F1993, F1994, ...

ℹ 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.
green_debt
# A tibble: 355 × 42
   ObjectId Country         ISO2  ISO3  Indicator Unit  Source CTS_Code CTS_Name
      <dbl> <chr>           <chr> <chr> <chr>     <chr> <chr>  <chr>    <chr>   
 1        1 Argentina       AR    ARG   Green Bo… Bill… Refin… ECFFI    Green B…
 2        2 Australia       AU    AUS   Green Bo… Bill… Refin… ECFFI    Green B…
 3        3 Austria         AT    AUT   Green Bo… Bill… Refin… ECFFI    Green B…
 4        4 Austria         AT    AUT   Sovereig… Bill… Refin… ECFF     Green B…
 5        5 Bangladesh      BD    BGD   Green Bo… Bill… Refin… ECFFI    Green B…
 6        6 Belarus, Rep. … BY    BLR   Green Bo… Bill… Refin… ECFFI    Green B…
 7        7 Belarus, Rep. … BY    BLR   Sovereig… Bill… Refin… ECFF     Green B…
 8        8 Belgium         BE    BEL   Green Bo… Bill… Refin… ECFFI    Green B…
 9        9 Belgium         BE    BEL   Sovereig… Bill… Refin… ECFF     Green B…
10       10 Bermuda         BM    BMU   Green Bo… Bill… Refin… ECFFI    Green B…
# ℹ 345 more rows
# ℹ 33 more variables: CTS_Full_Descriptor <chr>, Type_of_Issuer <chr>,
#   Use_of_Proceed <chr>, Principal_Currency <chr>, F1985 <dbl>, F1986 <dbl>,
#   F1987 <dbl>, F1990 <dbl>, F1991 <dbl>, F1992 <dbl>, F1993 <dbl>,
#   F1994 <dbl>, F1999 <dbl>, F2000 <dbl>, F2002 <dbl>, F2003 <dbl>,
#   F2004 <dbl>, F2007 <dbl>, F2008 <dbl>, F2009 <dbl>, F2010 <dbl>,
#   F2011 <dbl>, F2012 <dbl>, F2013 <dbl>, F2014 <dbl>, F2015 <dbl>, …
indicators_we_want <- c("Green Bond Issuances by Country", "Sovereign Green Bond Issuances")

green_debt_subset <- green_debt %>%
  clean_names() %>%
  filter(indicator %in% indicators_we_want) %>%
  select(country, iso3, indicator, matches("f\\d{4}"))
green_debt_subset
# A tibble: 107 × 32
   country iso3  indicator f1985 f1986 f1987 f1990 f1991 f1992 f1993 f1994 f1999
   <chr>   <chr> <chr>     <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1 Argent… ARG   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 2 Austra… AUS   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 3 Austria AUT   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 4 Austria AUT   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 5 Bangla… BGD   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 6 Belaru… BLR   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 7 Belaru… BLR   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 8 Belgium BEL   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
 9 Belgium BEL   Sovereig…    NA    NA    NA    NA    NA    NA    NA    NA    NA
10 Bermuda BMU   Green Bo…    NA    NA    NA    NA    NA    NA    NA    NA    NA
# ℹ 97 more rows
# ℹ 20 more variables: f2000 <dbl>, f2002 <dbl>, f2003 <dbl>, f2004 <dbl>,
#   f2007 <dbl>, f2008 <dbl>, f2009 <dbl>, f2010 <dbl>, f2011 <dbl>,
#   f2012 <dbl>, f2013 <dbl>, f2014 <dbl>, f2015 <dbl>, f2016 <dbl>,
#   f2017 <dbl>, f2018 <dbl>, f2019 <dbl>, f2020 <dbl>, f2021 <dbl>,
#   f2022 <dbl>
green_debt_subset %>%
  pivot_longer(cols = matches("f\\d{4}"))
# A tibble: 3,103 × 5
   country   iso3  indicator                       name  value
   <chr>     <chr> <chr>                           <chr> <dbl>
 1 Argentina ARG   Green Bond Issuances by Country f1985    NA
 2 Argentina ARG   Green Bond Issuances by Country f1986    NA
 3 Argentina ARG   Green Bond Issuances by Country f1987    NA
 4 Argentina ARG   Green Bond Issuances by Country f1990    NA
 5 Argentina ARG   Green Bond Issuances by Country f1991    NA
 6 Argentina ARG   Green Bond Issuances by Country f1992    NA
 7 Argentina ARG   Green Bond Issuances by Country f1993    NA
 8 Argentina ARG   Green Bond Issuances by Country f1994    NA
 9 Argentina ARG   Green Bond Issuances by Country f1999    NA
10 Argentina ARG   Green Bond Issuances by Country f2000    NA
# ℹ 3,093 more rows
green_bonds_tidy <- green_debt_subset %>%
  pivot_longer(cols = matches("f\\d{4}"), names_to = "year", values_to = "issuance_bn_usd", names_transform = readr::parse_number, values_drop_na = TRUE)
green_bonds_tidy
# A tibble: 465 × 5
   country   iso3  indicator                        year issuance_bn_usd
   <chr>     <chr> <chr>                           <dbl>           <dbl>
 1 Argentina ARG   Green Bond Issuances by Country  2017          0.974 
 2 Argentina ARG   Green Bond Issuances by Country  2020          0.0500
 3 Argentina ARG   Green Bond Issuances by Country  2021          0.916 
 4 Argentina ARG   Green Bond Issuances by Country  2022          0.207 
 5 Australia AUS   Green Bond Issuances by Country  2014          0.526 
 6 Australia AUS   Green Bond Issuances by Country  2015          0.413 
 7 Australia AUS   Green Bond Issuances by Country  2016          0.531 
 8 Australia AUS   Green Bond Issuances by Country  2017          2.53  
 9 Australia AUS   Green Bond Issuances by Country  2018          2.22  
10 Australia AUS   Green Bond Issuances by Country  2019          1.98  
# ℹ 455 more rows
green_bonds_tidy_cumulative <- green_bonds_tidy %>%
  select(-iso3) %>%
  arrange(country, year) %>%
  group_by(country, indicator) %>%
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd)) %>%
  ungroup()
green_bonds_tidy_cumulative
# A tibble: 465 × 5
   country   indicator                    year issuance_bn_usd cumulative_bn_usd
   <chr>     <chr>                       <dbl>           <dbl>             <dbl>
 1 Argentina Green Bond Issuances by Co…  2017          0.974              0.974
 2 Argentina Green Bond Issuances by Co…  2020          0.0500             1.02 
 3 Argentina Green Bond Issuances by Co…  2021          0.916              1.94 
 4 Argentina Green Bond Issuances by Co…  2022          0.207              2.15 
 5 Australia Green Bond Issuances by Co…  2014          0.526              0.526
 6 Australia Green Bond Issuances by Co…  2015          0.413              0.938
 7 Australia Green Bond Issuances by Co…  2016          0.531              1.47 
 8 Australia Green Bond Issuances by Co…  2017          2.53               4.00 
 9 Australia Green Bond Issuances by Co…  2018          2.22               6.22 
10 Australia Green Bond Issuances by Co…  2019          1.98               8.21 
# ℹ 455 more rows
biggest_green_bond_issuers <- green_bonds_tidy_cumulative %>%
  filter(indicator == "Green Bond Issuances by Country") %>%
  group_by(country) %>%
  slice_max(order_by = year) %>%
  arrange(cumulative_bn_usd %>% desc()) %>%
  select(country, cumulative_bn_usd) %>%
  ungroup()
biggest_green_bond_issuers
# A tibble: 79 × 2
   country               cumulative_bn_usd
   <chr>                             <dbl>
 1 China, P.R.: Mainland             325. 
 2 Germany                           253. 
 3 France                            213. 
 4 United States                     172. 
 5 Netherlands, The                  155. 
 6 United Kingdom                     84.0
 7 Sweden                             76.6
 8 Spain                              65.7
 9 Japan                              61.8
10 Italy                              59.9
# ℹ 69 more rows
top_10_chart <- biggest_green_bond_issuers %>%
  slice_max(order_by = cumulative_bn_usd, n = 10) %>%
  ggplot(aes(x = cumulative_bn_usd, y = fct_reorder(.f = country, .x = cumulative_bn_usd))) + geom_col(fill = "forestgreen")
top_10_chart

top_10_chart + theme_minimal() + scale_x_continuous(labels = scales::label_dollar(suffix = "bn"), expand = c(0,0)) +
  labs(title = "Top 10 Green Bond Issuers", subtitle = "The Dutch sure do issue a whole lot of green bonds per capita",
       x = "Cumulative Issuance (USD)",
       y = "",
       caption = "Data: IMF Climate Change Dashboard | Insight: Me!" )

green_bonds_tidy %>%
  pivot_wider(names_from = indicator, values_from = issuance_bn_usd)
# A tibble: 420 × 5
   country   iso3   year Green Bond Issuances by Countr…¹ Sovereign Green Bond…²
   <chr>     <chr> <dbl>                            <dbl>                  <dbl>
 1 Argentina ARG    2017                           0.974                      NA
 2 Argentina ARG    2020                           0.0500                     NA
 3 Argentina ARG    2021                           0.916                      NA
 4 Argentina ARG    2022                           0.207                      NA
 5 Australia AUS    2014                           0.526                      NA
 6 Australia AUS    2015                           0.413                      NA
 7 Australia AUS    2016                           0.531                      NA
 8 Australia AUS    2017                           2.53                       NA
 9 Australia AUS    2018                           2.22                       NA
10 Australia AUS    2019                           1.98                       NA
# ℹ 410 more rows
# ℹ abbreviated names: ¹​`Green Bond Issuances by Country`,
#   ²​`Sovereign Green Bond Issuances`
corporate_vs_sovereign_green_bonds <- green_bonds_tidy %>%
  select(-iso3) %>%
  pivot_wider(names_from = indicator, values_from = issuance_bn_usd, values_fill = 0) %>%
  clean_names() %>%
  mutate(corporate_green_bond_issuances = green_bond_issuances_by_country - sovereign_green_bond_issuances)
corporate_vs_sovereign_green_bonds
# A tibble: 420 × 5
   country    year green_bond_issuances_by_country sovereign_green_bond_issuan…¹
   <chr>     <dbl>                           <dbl>                         <dbl>
 1 Argentina  2017                          0.974                              0
 2 Argentina  2020                          0.0500                             0
 3 Argentina  2021                          0.916                              0
 4 Argentina  2022                          0.207                              0
 5 Australia  2014                          0.526                              0
 6 Australia  2015                          0.413                              0
 7 Australia  2016                          0.531                              0
 8 Australia  2017                          2.53                               0
 9 Australia  2018                          2.22                               0
10 Australia  2019                          1.98                               0
# ℹ 410 more rows
# ℹ abbreviated name: ¹​sovereign_green_bond_issuances
# ℹ 1 more variable: corporate_green_bond_issuances <dbl>
corporate_vs_sovereign_green_bonds %>%
  pivot_longer(cols = contains("green_bond"))
# A tibble: 1,260 × 4
   country    year name                             value
   <chr>     <dbl> <chr>                            <dbl>
 1 Argentina  2017 green_bond_issuances_by_country 0.974 
 2 Argentina  2017 sovereign_green_bond_issuances  0     
 3 Argentina  2017 corporate_green_bond_issuances  0.974 
 4 Argentina  2020 green_bond_issuances_by_country 0.0500
 5 Argentina  2020 sovereign_green_bond_issuances  0     
 6 Argentina  2020 corporate_green_bond_issuances  0.0500
 7 Argentina  2021 green_bond_issuances_by_country 0.916 
 8 Argentina  2021 sovereign_green_bond_issuances  0     
 9 Argentina  2021 corporate_green_bond_issuances  0.916 
10 Argentina  2022 green_bond_issuances_by_country 0.207 
# ℹ 1,250 more rows
snake_case_to_title_case <- function(input_string) {
  
  input_string |> 
    str_replace_all(pattern = "_", replacement = " ") |>
    str_to_title()
}

corporate_vs_sovereign_green_bonds_long <- corporate_vs_sovereign_green_bonds |> 
  pivot_longer(cols = contains("green_bond"),
               names_to = "indicator",
               values_to = "issuance_bn_usd",
               names_transform = snake_case_to_title_case) 

corporate_vs_sovereign_green_bonds_long
# A tibble: 1,260 × 4
   country    year indicator                       issuance_bn_usd
   <chr>     <dbl> <chr>                                     <dbl>
 1 Argentina  2017 Green Bond Issuances By Country          0.974 
 2 Argentina  2017 Sovereign Green Bond Issuances           0     
 3 Argentina  2017 Corporate Green Bond Issuances           0.974 
 4 Argentina  2020 Green Bond Issuances By Country          0.0500
 5 Argentina  2020 Sovereign Green Bond Issuances           0     
 6 Argentina  2020 Corporate Green Bond Issuances           0.0500
 7 Argentina  2021 Green Bond Issuances By Country          0.916 
 8 Argentina  2021 Sovereign Green Bond Issuances           0     
 9 Argentina  2021 Corporate Green Bond Issuances           0.916 
10 Argentina  2022 Green Bond Issuances By Country          0.207 
# ℹ 1,250 more rows
china_vs_germany_green_bonds <- corporate_vs_sovereign_green_bonds_long |> 
  filter(country %in% c("China, P.R.: Mainland", "Germany")) |> 
  arrange(year) |> 
  pivot_wider(names_from = country,
              values_from = issuance_bn_usd,
              values_fill = 0) |> 
  clean_names() 

china_vs_germany_green_bonds
# A tibble: 48 × 4
    year indicator                       germany china_p_r_mainland
   <dbl> <chr>                             <dbl>              <dbl>
 1  1991 Green Bond Issuances By Country  0.0292                  0
 2  1991 Sovereign Green Bond Issuances   0                       0
 3  1991 Corporate Green Bond Issuances   0.0292                  0
 4  1992 Green Bond Issuances By Country  0.0350                  0
 5  1992 Sovereign Green Bond Issuances   0                       0
 6  1992 Corporate Green Bond Issuances   0.0350                  0
 7  1993 Green Bond Issuances By Country  0.0175                  0
 8  1993 Sovereign Green Bond Issuances   0                       0
 9  1993 Corporate Green Bond Issuances   0.0175                  0
10  2000 Green Bond Issuances By Country  0.0272                  0
# ℹ 38 more rows
china_vs_germany_green_bonds_repeatable_factoids <- china_vs_germany_green_bonds |> 
  mutate(
    germany_x_than_china = germany/china_p_r_mainland,
    china_x_than_germany = china_p_r_mainland/germany,
    germany_pct_more_than_china = (germany/china_p_r_mainland-1) * 100,
    china_pct_more_than_germany = (china_p_r_mainland/germany-1) * 100
)

china_vs_germany_green_bonds_repeatable_factoids
# A tibble: 48 × 8
    year indicator               germany china_p_r_mainland germany_x_than_china
   <dbl> <chr>                     <dbl>              <dbl>                <dbl>
 1  1991 Green Bond Issuances B…  0.0292                  0                  Inf
 2  1991 Sovereign Green Bond I…  0                       0                  NaN
 3  1991 Corporate Green Bond I…  0.0292                  0                  Inf
 4  1992 Green Bond Issuances B…  0.0350                  0                  Inf
 5  1992 Sovereign Green Bond I…  0                       0                  NaN
 6  1992 Corporate Green Bond I…  0.0350                  0                  Inf
 7  1993 Green Bond Issuances B…  0.0175                  0                  Inf
 8  1993 Sovereign Green Bond I…  0                       0                  NaN
 9  1993 Corporate Green Bond I…  0.0175                  0                  Inf
10  2000 Green Bond Issuances B…  0.0272                  0                  Inf
# ℹ 38 more rows
# ℹ 3 more variables: china_x_than_germany <dbl>,
#   germany_pct_more_than_china <dbl>, china_pct_more_than_germany <dbl>
factoid_2014 <- china_vs_germany_green_bonds_repeatable_factoids |> 
  filter(year == 2014) |> 
  filter(indicator == "Green Bond Issuances By Country") |> 
  pull(germany_x_than_china) |> 
  round()

factoid_2022 <- china_vs_germany_green_bonds_repeatable_factoids |> 
  filter(year == 2022) |> 
  filter(indicator == "Green Bond Issuances By Country") |> 
  pull(china_pct_more_than_germany) |> 
  round()

factoid_2022
[1] 19
#Question 1 (Done)

region_cumulative <- green_debt_subset %>%
  filter(indicator %in% indicators_we_want) %>%
  pivot_longer(cols = matches("f\\d{4}"), names_to = "year", values_to = "issuance_bn_usd", names_transform = readr::parse_number, values_drop_na = TRUE) %>%
  mutate(regional = countrycode(country, "country.name", "region"))%>%
  group_by(regional) %>%
  summarize(cumulative_bn_usd = cumsum(issuance_bn_usd))
Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
  always returns an ungrouped data frame and adjust accordingly.
`summarise()` has grouped output by 'regional'. You can override using the
`.groups` argument.
region_cumulative
# A tibble: 465 × 2
# Groups:   regional [7]
   regional            cumulative_bn_usd
   <chr>                           <dbl>
 1 East Asia & Pacific             0.526
 2 East Asia & Pacific             0.938
 3 East Asia & Pacific             1.47 
 4 East Asia & Pacific             4.00 
 5 East Asia & Pacific             6.22 
 6 East Asia & Pacific             8.21 
 7 East Asia & Pacific            11.1  
 8 East Asia & Pacific            15.8  
 9 East Asia & Pacific            20.1  
10 East Asia & Pacific            20.4  
# ℹ 455 more rows
ggplot(region_cumulative, aes(x = regional, y = cumulative_bn_usd, color = regional)) + geom_point() +labs(title = "Cumulative Green Debt Issuances in USD Billions per Region", x = "Region", y = "Cumulative Bn USD")

#Question 2
green_debt_working_1 <- green_debt %>%
  clean_names() %>%
  select(type_of_issuer, country, matches("f\\d{4}")) %>%
  arrange("f\\d{4}") %>%
  filter(type_of_issuer != "Not Applicable") %>%
  pivot_longer(cols = matches("f\\d{4}"), names_to = "year", values_to = "issuance_bn_usd") %>%
  drop_na("issuance_bn_usd") %>%
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd))

green_debt_working_1
# A tibble: 98 × 5
   type_of_issuer country year  issuance_bn_usd cumulative_bn_usd
   <chr>          <chr>   <chr>           <dbl>             <dbl>
 1 Banks          World   f1985          0.03               0.03 
 2 Banks          World   f1986          0.3                0.33 
 3 Banks          World   f1987          0.0751             0.405
 4 Banks          World   f1990          0                  0.405
 5 Banks          World   f1999          0.0482             0.453
 6 Banks          World   f2000          0.0533             0.507
 7 Banks          World   f2002          0.148              0.654
 8 Banks          World   f2003          0.01               0.664
 9 Banks          World   f2004          0.109              0.773
10 Banks          World   f2013          0.757              1.53 
# ℹ 88 more rows
green_debt_working_1_factoid_1 <- green_debt_working_1 %>%
  filter(type_of_issuer %in% c("Banks", "International Organization")) %>%
  group_by(year, type_of_issuer) %>%
  summarize(issuance_bn_usd = sum(issuance_bn_usd)) %>%
  pivot_wider(names_from = type_of_issuer, values_from = issuance_bn_usd, values_fill = 0) %>%
  clean_names()
`summarise()` has grouped output by 'year'. You can override using the
`.groups` argument.
print(green_debt_working_1_factoid_1, n = 25)
# A tibble: 25 × 3
# Groups:   year [25]
   year     banks international_organization
   <chr>    <dbl>                      <dbl>
 1 f1985   0.03                        0    
 2 f1986   0.3                         0    
 3 f1987   0.0751                      0    
 4 f1990   0                           0    
 5 f1999   0.0482                      0    
 6 f2000   0.0533                      0    
 7 f2002   0.148                       0    
 8 f2003   0.01                        0    
 9 f2004   0.109                       0    
10 f2007   0                           1.46 
11 f2008   0                           0.371
12 f2009   0                           0.890
13 f2010   0                           2.54 
14 f2011   0                           0.763
15 f2012   0                           1.44 
16 f2013   0.757                       7.18 
17 f2014   1.10                       10.5  
18 f2015   7.83                        7.27 
19 f2016  32.0                        16.7  
20 f2017  27.8                         9.66 
21 f2018  45.5                        17.1  
22 f2019  43.5                        19.8  
23 f2020  37.7                        20.7  
24 f2021  92.8                        35.6  
25 f2022 154.                         35.6  
actual_gd_factoid_calc <- green_debt_working_1_factoid_1 %>%
  mutate(
    bank_ratio = banks/(international_organization + banks),
    IO_ratio = international_organization/(international_organization + banks)
  )
actual_gd_factoid_calc
# A tibble: 25 × 5
# Groups:   year [25]
   year   banks international_organization bank_ratio IO_ratio
   <chr>  <dbl>                      <dbl>      <dbl>    <dbl>
 1 f1985 0.03                         0             1        0
 2 f1986 0.3                          0             1        0
 3 f1987 0.0751                       0             1        0
 4 f1990 0                            0           NaN      NaN
 5 f1999 0.0482                       0             1        0
 6 f2000 0.0533                       0             1        0
 7 f2002 0.148                        0             1        0
 8 f2003 0.01                         0             1        0
 9 f2004 0.109                        0             1        0
10 f2007 0                            1.46          0        1
# ℹ 15 more rows
print(actual_gd_factoid_calc, n = 25)
# A tibble: 25 × 5
# Groups:   year [25]
   year     banks international_organization bank_ratio IO_ratio
   <chr>    <dbl>                      <dbl>      <dbl>    <dbl>
 1 f1985   0.03                        0         1         0    
 2 f1986   0.3                         0         1         0    
 3 f1987   0.0751                      0         1         0    
 4 f1990   0                           0       NaN       NaN    
 5 f1999   0.0482                      0         1         0    
 6 f2000   0.0533                      0         1         0    
 7 f2002   0.148                       0         1         0    
 8 f2003   0.01                        0         1         0    
 9 f2004   0.109                       0         1         0    
10 f2007   0                           1.46      0         1    
11 f2008   0                           0.371     0         1    
12 f2009   0                           0.890     0         1    
13 f2010   0                           2.54      0         1    
14 f2011   0                           0.763     0         1    
15 f2012   0                           1.44      0         1    
16 f2013   0.757                       7.18      0.0954    0.905
17 f2014   1.10                       10.5       0.0945    0.905
18 f2015   7.83                        7.27      0.518     0.482
19 f2016  32.0                        16.7       0.657     0.343
20 f2017  27.8                         9.66      0.742     0.258
21 f2018  45.5                        17.1       0.727     0.273
22 f2019  43.5                        19.8       0.687     0.313
23 f2020  37.7                        20.7       0.645     0.355
24 f2021  92.8                        35.6       0.723     0.277
25 f2022 154.                         35.6       0.812     0.188
factoid_2015_q2 <- actual_gd_factoid_calc %>%
  filter(year == 2015) %>%
  pull(bank_ratio)

factoid_2015_q2
numeric(0)
ggplot(green_debt_working_1, aes(x = fct_reorder(.f = type_of_issuer, .x = cumulative_bn_usd), y = cumulative_bn_usd, fill = type_of_issuer)) +
  geom_col() +
  labs(title = "Cumulative Amount of Issued Funds per Type of Issuer", x = "Type of Issuer", y = "Billions of Dollars in Funds Issued")

#Question 3

green_debt_working_A<- green_debt %>%
  clean_names() %>%
  select(use_of_proceed, country, matches("f\\d{4}")) %>%
  arrange("f\\d{4}") %>%
  filter(use_of_proceed != "Not Applicable") %>%
  pivot_longer(cols = matches("f\\d{4}"), names_to = "year", values_to = "issuance_bn_usd") %>%
  drop_na("issuance_bn_usd") %>%
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd)) %>%
  slice_max(issuance_bn_usd, n = 10)
green_debt_working_A
# A tibble: 10 × 5
   use_of_proceed                country year  issuance_bn_usd cumulative_bn_usd
   <chr>                         <chr>   <chr>           <dbl>             <dbl>
 1 Clean Transport               World   f2022           833.             1002. 
 2 Energy Efficiency             World   f2022           480.             1966. 
 3 Climate Change Adaptation     World   f2022           277.             1279. 
 4 Eligible Green Projects       World   f2022           204.             1485. 
 5 Green Construction/Buildings  World   f2022           127.             2111. 
 6 Renewable Energy Projects     World   f2022           108.             2252. 
 7 Aquatic Biodiversity Conserv… World   f2022            75.7             123. 
 8 Circular Economy Adapted/Eco… World   f2022            33.6             168. 
 9 Alternative Energy            World   f2022            25.7              46.9
10 Sustainable Water or Wastewa… World   f2022            17.7            2277. 
ggplot(green_debt_working_A, aes(x = fct_reorder(.f = use_of_proceed, .x = issuance_bn_usd), y = issuance_bn_usd, color = use_of_proceed)) +
  geom_point() +
  labs(title = "Amount of Issued Funds per Use of Proceeds", x = "Top 10 Largest Uses of Proceeds", y = "Billions of Dollars")

# Q3 P2

green_debt_working_B <- green_debt %>%
  clean_names() %>%
  select(principal_currency, country, matches("f\\d{4}")) %>%
  arrange("f\\d{4}") %>%
  filter(principal_currency != "Not Applicable") %>%
  pivot_longer(cols = matches("f\\d{4}"), names_to = "year", values_to = "issuance_bn_usd") %>%
  drop_na("issuance_bn_usd") %>%
  mutate(cumulative_bn_usd = cumsum(issuance_bn_usd)) %>%
  slice_max(issuance_bn_usd, n = 10)
green_debt_working_B
# A tibble: 10 × 5
   principal_currency country year  issuance_bn_usd cumulative_bn_usd
   <chr>              <chr>   <chr>           <dbl>             <dbl>
 1 Euro               World   f2022           976.             1483. 
 2 US Dollar          World   f2022           580.             2285. 
 3 Chinese Yuan       World   f2022           325.              485. 
 4 Swedish Krona      World   f2022            73.3            1672. 
 5 British Pound      World   f2022            73.2             106. 
 6 Canadian Dollar    World   f2022            53.1             160. 
 7 Japanese Yen       World   f2022            33.8            1534. 
 8 Australian Dollar  World   f2022            27.8              27.9
 9 Swiss Franc        World   f2022            22.4            1695. 
10 Danish Krone       World   f2022            20.3             507. 
ggplot(green_debt_working_B, aes(x = fct_reorder(.f = principal_currency, .x = issuance_bn_usd), y = issuance_bn_usd, color = issuance_bn_usd)) +
  geom_point() +
  labs(title = "Amount of Issued Funds per Currency", x = "Top 10 Most Commonly Used Currencies in Green Debt Issuance", y = "Billions of Dollars in Funds Issued")