Project 2: Data Tidying

World Bank GDP Per Capita

Author

Jocelyn Slater

Published

October 9, 2026

Introducion

Esra Dogan suggested looking at the GDP per capita data from the World Bank. The raw data was very messy with lots of missing data, so before any analysis could be done, we first needed to tidy the dataframe. Then we investigated which countries had the fastest growth in recent years.

Import Data

Import data from github hosted .csv and glimpse what we are working with.

Code
url <- "https://raw.githubusercontent.com/jocslater-code/DATA607/refs/heads/main/Project2/WorldBankGDP/API_NY.GDP.PCAP.CD_DS2_en_csv_v2_440845.csv"
df_raw <- read.csv(url, skip = 4)
head(df_raw)
                 Country.Name Country.Code               Indicator.Name
1                       Aruba          ABW GDP per capita (current US$)
2 Africa Eastern and Southern          AFE GDP per capita (current US$)
3                 Afghanistan          AFG GDP per capita (current US$)
4  Africa Western and Central          AFW GDP per capita (current US$)
5                      Angola          AGO GDP per capita (current US$)
6                     Albania          ALB GDP per capita (current US$)
  Indicator.Code    X1960    X1961    X1962    X1963    X1964    X1965    X1966
1 NY.GDP.PCAP.CD       NA       NA       NA       NA       NA       NA       NA
2 NY.GDP.PCAP.CD 186.0895 186.9094 197.3679 225.4005 208.9631 226.8365 240.9122
3 NY.GDP.PCAP.CD       NA       NA       NA       NA       NA       NA       NA
4 NY.GDP.PCAP.CD 121.9368 127.4510 133.8238 139.0050 148.5515 155.5879 162.1730
5 NY.GDP.PCAP.CD       NA       NA       NA       NA       NA       NA       NA
6 NY.GDP.PCAP.CD       NA       NA       NA       NA       NA       NA       NA
     X1967    X1968    X1969    X1970    X1971    X1972    X1973    X1974
1       NA       NA       NA       NA       NA       NA       NA       NA
2 243.7740 257.1443 281.5794 276.7334 294.8147 311.4656 389.7297 463.4695
3       NA       NA       NA       NA       NA       NA       NA       NA
4 145.0468 146.3454 162.0830 218.8430 196.0138 230.2506 280.8667 368.5883
5       NA       NA       NA       NA       NA       NA       NA       NA
6       NA       NA       NA       NA       NA       NA       NA       NA
     X1975    X1976    X1977    X1978    X1979    X1980     X1981     X1982
1       NA       NA       NA       NA       NA       NA        NA        NA
2 479.0796 468.7753 518.3596 571.6183 634.4492 773.3025  777.6293  725.5487
3       NA       NA       NA       NA       NA       NA        NA        NA
4 413.6970 480.8334 491.2356 524.2053 625.3780 763.6332 1329.8786 1165.2487
5       NA       NA       NA       NA       NA 729.1120  657.9826  634.2215
6       NA       NA       NA       NA       NA 590.6077  663.2942  668.4545
     X1983    X1984    X1985     X1986     X1987      X1988      X1989
1       NA       NA       NA 6767.5592 8244.0457 10056.2614 11507.2172
2 732.3467 650.3479 554.2222  578.3184  664.7929   704.1432   728.2445
3       NA       NA       NA        NA        NA         NA         NA
4 874.9380 739.4898 755.9250  583.6667  584.1036   563.8791   513.1731
5 636.8328 650.4911 772.4688  697.5266  770.1011   807.4396   907.7479
6 661.5468 639.4847 639.8659  693.8735  674.7934   652.7743   697.9956
       X1990      X1991      X1992      X1993      X1994      X1995      X1996
1 12187.5364 13233.9905 13892.6051 14700.9598 16055.2878 16548.7174 16620.9546
2   822.4044   864.1755   732.9121   709.3247   700.7733   766.5242   746.7886
3         NA         NA         NA         NA         NA         NA         NA
4   593.8315   608.6690   567.9263   575.2817   580.8015   867.8187  1070.8362
5   965.8668   881.9195   668.7060   449.7279   334.9736   404.2948   531.1154
6   617.2304   336.5870   200.8522   367.2792   586.4161   911.3205  1020.9762
       X1997      X1998      X1999      X2000      X2001      X2002      X2003
1 17750.0096 18828.0871 19216.1972 20681.0230 20740.1326 21307.2483 21949.4860
2   767.4007   697.0806   670.4253   706.7284   625.8289   630.5134   815.4337
3         NA         NA         NA   174.9310   138.7068   178.9541   198.8711
4  1093.0672  1142.8445   524.4531   519.6426   533.6772   620.1607   698.4106
5   521.7029   429.1881   392.7255   563.7338   533.5862   999.0659  1133.6633
6   728.5455   831.1753  1056.3448  1160.4205  1326.4165  1479.8388  1908.6990
       X2004      X2005      X2006      X2007      X2008      X2009      X2010
1 23700.6320 24171.8371 24845.6585 26736.3089 28171.9094 25134.7712 24093.1402
2   989.0171  1126.2992  1235.1709  1381.4453  1447.4278  1408.6157  1628.7686
3   221.7637   254.1842   274.2186   376.2232   381.7332   452.0537   560.6215
4   839.3459  1001.1345  1236.3160  1407.7288  1668.6157  1455.2239  1664.4920
5  1451.4712  2145.8862  2930.4443  3515.0568  4578.1553  3645.1485  4101.6372
6  2446.9095  2741.7214  3057.7726  3743.0553  4498.5049  4213.6501  4149.1447
       X2011      X2012      X2013      X2014      X2015      X2016      X2017
1 25712.3843 25119.6655 25813.5714 26129.8391 27458.2202 27441.5502 28440.0417
2  1761.8383  1731.8691  1705.8390  1689.9824  1498.7160  1335.7432  1529.9231
3   606.6947   651.4171   637.0871   625.0549   565.5697   522.0822   525.4698
4  1846.1029  1943.3726  2134.2475  2224.3296  1863.4015  1632.4250  1577.2035
5  5184.1527  5702.4531  5688.5792  5649.6876  3641.7289  2082.3734  2832.1500
6  4465.7091  4280.9332  4542.9290  4793.5975  4199.5391  4457.6341  5006.3601
       X2018      X2019      X2020      X2021      X2022      X2023      X2024
1 30082.1584 30654.4851 22664.3710 26827.3448 31000.5714 34897.6184 38590.5650
2  1553.6179  1508.0315  1351.5032  1560.8946  1675.9025  1571.1327  1628.2273
3   491.3372   496.6025   510.7871   356.4962   357.2612   413.7579   416.8711
4  1723.1145  2219.4126  2034.4379  2116.9383  2143.0721  1846.2468  1416.2284
5  2891.8303  2507.8681  1749.1795  2266.9683  3598.5367  2885.5135  2720.8190
6  5897.6545  6069.4390  6027.9135  7242.4551  7756.9619  9740.7023 11374.0086
      X2025  X
1        NA NA
2  1722.386 NA
3        NA NA
4  1600.058 NA
5  3129.477 NA
6 12998.148 NA

Tidy Data

The data needs to be formatted for analysis. I first cleaned up the column names using the Janitor library, coverted from wide to long, extracted clear years, and droped rows that have all NA values. Then I picked just the geographic data (regions) that I am interested in exploring.

Code
df_tidy <- df_raw %>%
  # Janitor to clean up the column names
  clean_names() %>%
  
  # Pivot all year columns starting with 'x' into long format
  pivot_longer(
    cols = starts_with("x"),
    names_to = "year",
    values_to = "value"
  ) %>%
  
  # Extract years and cast as numeric
  mutate(
    # Remove the 'x' or 'x_' prefix and convert to numeric year
    year = as.numeric(str_remove(year, "^x_?"))
  ) %>%
  
  # Drop NAs
  filter(!is.na(value))


df_mini <- df_tidy %>% 
  filter(country_name %in% c("Middle East, North Africa, Afghanistan & Pakistan", "East Asia & Pacific", "European Union", "Latin America & Caribbean","North America" , "Africa Eastern and Southern", "Africa Western and Central"))

df_mini$country_name[df_mini$country_name == "Middle East, North Africa, Afghanistan & Pakistan"] <- "MENA"
df_mini$country_name[df_mini$country_name == "Africa Eastern and Southern"] <- "Southern and Eastern Africa"
df_mini$country_name[df_mini$country_name == "Africa Western and Central"] <- "Central and Western Africa"

head(df_mini)
# A tibble: 6 × 6
  country_name            country_code indicator_name indicator_code  year value
  <chr>                   <chr>        <chr>          <chr>          <dbl> <dbl>
1 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1960  186.
2 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1961  187.
3 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1962  197.
4 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1963  225.
5 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1964  209.
6 Southern and Eastern A… AFE          GDP per capit… NY.GDP.PCAP.CD  1965  227.

Analysis

We plotted all of the data to get a qualitative sense of overall trends and created summary tables to get a quantitative view of the GDP per capita data. GDP is in current (2026) US dollars.

Code
country_summary <- df_mini %>%
  group_by(country_name, country_code) %>%
  summarise(
    start_year = min(year, na.rm = TRUE),
    end_year   = max(year, na.rm = TRUE),
    min_gdp    = min(value, na.rm = TRUE),
    max_gdp    = max(value, na.rm = TRUE),
    mean_gdp   = mean(value, na.rm = TRUE),
    latest_gdp = value[which.max(year)],
    .groups = "drop"
  )

country_summary$start_year<- NULL
country_summary$end_year<- NULL

country_summary %>%
  gt() %>%
  tab_header(
    title = "GDP Per Capita Summary by Region",
  ) %>%
  fmt_currency(
    columns = c(min_gdp, max_gdp, mean_gdp, latest_gdp),
    currency = "USD",
    decimals = 2) %>%
  cols_label(
    country_name = "Country",
    country_code = "ISO Code",
    min_gdp      = "Min GDP",
    max_gdp      = "Max GDP",
    mean_gdp     = "Mean GDP",
    latest_gdp   = "Latest GDP"
  )
GDP Per Capita Summary by Region
Country ISO Code Min GDP Max GDP Mean GDP Latest GDP
Central and Western Africa AFW $121.94 $2,224.33 $920.25 $1,600.06
East Asia & Pacific EAS $151.80 $14,077.27 $4,398.41 $14,077.27
European Union EUU $797.72 $47,089.16 $17,604.60 $47,089.16
Latin America & Caribbean LCN $362.35 $11,095.22 $4,232.22 $11,095.22
MENA MEA $183.76 $6,436.74 $2,599.05 $6,202.19
North America NAC $2,933.33 $86,308.07 $30,223.67 $86,308.07
Southern and Eastern Africa AFE $186.09 $1,761.84 $861.38 $1,722.39
Code
ggplot(df_mini, aes(x = year, y = value, color = country_name)) +
  geom_line(linewidth = 1) +
  scale_y_continuous(labels = scales::dollar_format()) +
  labs(
    title = "GDP Per Capita Over Time by Region",
    x = "Year",
    y = "GDP per Capita (Current US$)",
    color = "Region"
  ) +
  theme_minimal()

I also want to calculate the percent change in GDP to see which region has grown the most over the time period

Code
pct_change_df <- df_mini %>%
  arrange(country_name, year) %>%
  group_by(country_name) %>%
  summarize(
    pct_change = ((last(value) - first(value)) / first(value)) * 100
  ) %>%
  arrange(desc(pct_change))

# Great table
pct_change_df %>%
  gt() %>%
  tab_header(
    title = "GDP Percent Change by Region",
  ) %>%
  fmt_number(
    columns = pct_change,
    decimals = 0
  ) %>%
  cols_label(
    country_name = "Country",
    pct_change = "Percent Change in GDP (%)"
  )
GDP Percent Change by Region
Country Percent Change in GDP (%)
East Asia & Pacific 9,174
European Union 5,803
MENA 3,275
Latin America & Caribbean 2,962
North America 2,842
Central and Western Africa 1,212
Southern and Eastern Africa 826

Discussion and Next Steps

The region with the largest percent change in GDP was East Asia & Pacific (9,173%) while the region with the smallest is Southern and Eastern Africa.

The next steps could be to expand this analysis to new regions, look at all countries, or break East Asia & Pacific down by individual countries.