Overview

I used World Bank data to see which countries contributed most to global external debt in the 2010s. The raw file was messy, so I cleaned it: standardized column names, reshaped years into a tidy format, removed non-country aggregates, and handled missing values. I then summarized totals with dplyr, plotted the top contributors with ggplot2, and printed two short takeaways.

Data

## [1] "Country Name"          "Country Code"          "Counterpart-Area Name"
## [4] "Counterpart-Area Code" "Series Name"           "Series Code"
## [1] 76921    68

Filter

# drop World Bank aggregate groups so only countries remain
agg_codes <- c(
  "WLD","HIC","UMC","LMC","LMY","MIC","LIC","OED","EUU","EMU",
  "EAP","ECA","LCN","MNA","SAS","SSA","NAC","ARB","IBD","IDB","IDX","IDT"
)

# Use either Time or Year if present
year_col <- if ("Time" %in% names(df)) "Time" else if ("Year" %in% names(df)) "Year" else NA

if (!is.na(year_col)) {
  # LONG case
  debt <- df %>%
    filter(`Series Name` == "External debt stocks, total (DOD, current US$)") %>%
    transmute(
      Country = `Country Name`,
      Code    = `Country Code`,
      Year    = as.numeric(.data[[year_col]]),
      DebtUS  = as.numeric(Value)
    ) %>%
    filter(Year >= 2010, Year <= 2019, !is.na(DebtUS)) %>%
    filter(!(Code %in% agg_codes)) %>%
    arrange(Country, Year) %>%
    select(Country, Year, DebtUS)
} else {
  year_cols <- grep("^(X)?(19|20)\\d{2}$", names(df), value = TRUE)
  debt <- df %>%
    filter(`Series Name` == "External debt stocks, total (DOD, current US$)") %>%
    select(`Country Name`, `Country Code`, all_of(year_cols)) %>%
    pivot_longer(cols = all_of(year_cols), names_to = "Year", values_to = "DebtUS") %>%
    mutate(
      Country = `Country Name`,
      Code    = `Country Code`,
      Year    = as.numeric(sub("^X","", Year)),
      DebtUS  = as.numeric(DebtUS)
    ) %>%
    filter(Year >= 2010, Year <= 2019, !is.na(DebtUS)) %>%
    filter(!(Code %in% agg_codes)) %>%
    select(Country, Year, DebtUS) %>%
    arrange(Country, Year)
}

head(debt)
## # A tibble: 6 × 3
##   Country      Year     DebtUS
##   <chr>       <dbl>      <dbl>
## 1 Afghanistan  2010 2435844906
## 2 Afghanistan  2011 2485331046
## 3 Afghanistan  2012 2580623924
## 4 Afghanistan  2013 2587774667
## 5 Afghanistan  2014 2529865268
## 6 Afghanistan  2015 2596917266

Summarize

# total by country across the decade
by_country <- debt %>%
  group_by(Country) %>%
  summarise(TotalDebt_2010s = sum(DebtUS, na.rm = TRUE), .groups = "drop") %>%
  arrange(desc(TotalDebt_2010s)) %>%
  mutate(SharePct = 100 * TotalDebt_2010s / sum(TotalDebt_2010s, na.rm = TRUE))

# simple count example
per_year <- debt %>% count(Year, name = "CountriesReporting")

head(by_country)
## # A tibble: 6 × 3
##   Country                                           TotalDebt_2010s SharePct
##   <chr>                                                       <dbl>    <dbl>
## 1 Latin America & Caribbean (excluding high income)         1.54e13    18.3 
## 2 China                                                     1.47e13    17.6 
## 3 IDA total                                                 6.02e12     7.18
## 4 Mexico                                                    5.03e12     5.99
## 5 Brazil                                                    4.99e12     5.95
## 6 India                                                     4.43e12     5.28
head(per_year)
## # A tibble: 6 × 2
##    Year CountriesReporting
##   <dbl>              <int>
## 1  2010                120
## 2  2011                120
## 3  2012                121
## 4  2013                121
## 5  2014                121
## 6  2015                123

Plot

top10 <- by_country %>% slice(1:10)

ggplot(top10, aes(x = reorder(Country, SharePct), y = SharePct)) +
  geom_col() +
  coord_flip() +
  labs(title = "Top contributors to external debt (2010–2019)",
       x = "Country", y = "Share of decade total (%)") +
  theme_minimal()

Findings

# Two quick insights
top_country <- by_country$Country[1]
top_share   <- round(by_country$SharePct[1], 1)
print(paste("Insight 1: The largest contributor in the 2010s was", top_country,
            "with ~", top_share, "% of the decade total."))
## [1] "Insight 1: The largest contributor in the 2010s was Latin America & Caribbean (excluding high income) with ~ 18.3 % of the decade total."
median_top10 <- round(median(top10$SharePct, na.rm = TRUE), 1)
print(paste("Insight 2: Among the top 10, the typical (median) country held ~",
            median_top10, "% share, showing concentration at the top."))
## [1] "Insight 2: Among the top 10, the typical (median) country held ~ 5.6 % share, showing concentration at the top."
# show the exact top 10 table 
top10 %>% select(Country, TotalDebt_2010s, SharePct)
## # A tibble: 10 × 3
##    Country                                           TotalDebt_2010s SharePct
##    <chr>                                                       <dbl>    <dbl>
##  1 Latin America & Caribbean (excluding high income)         1.54e13    18.3 
##  2 China                                                     1.47e13    17.6 
##  3 IDA total                                                 6.02e12     7.18
##  4 Mexico                                                    5.03e12     5.99
##  5 Brazil                                                    4.99e12     5.95
##  6 India                                                     4.43e12     5.28
##  7 Turkiye                                                   3.90e12     4.64
##  8 Least developed countries: UN classification              3.17e12     3.78
##  9 Indonesia                                                 2.99e12     3.56
## 10 Argentina                                                 1.93e12     2.30