Read the data files

w_data <- read.csv('World_pop_data_2023-11-14 11_56_30.csv')
head(w_data)
##   Country.Code      Indicator.Name Indicator.Code     X1960     X1961     X1962
## 1          ABW "Population, total"    SP.POP.TOTL     54208     55434     56234
## 2          AFE "Population, total"    SP.POP.TOTL 130836765 134159786 137614644
## 3          AFG "Population, total"    SP.POP.TOTL   8996967   9169406   9351442
## 4          AFW "Population, total"    SP.POP.TOTL  96396419  98407221 100506960
## 5          AGO "Population, total"    SP.POP.TOTL   5454938   5531451   5608499
## 6          ALB "Population, total"    SP.POP.TOTL   1608800   1659800   1711319
##       X1963     X1964     X1965     X1966     X1967     X1968     X1969
## 1     56699     57029     57357     57702     58044     58377     58734
## 2 141202036 144920186 148769974 152752671 156876454 161156430 165611760
## 3   9543200   9744772   9956318  10174840  10399936  10637064  10893772
## 4 102691339 104953470 107289875 109701811 112195950 114781116 117468741
## 5   5679409   5734995   5770573   5781305   5774440   5771973   5803677
## 6   1762621   1814135   1864791   1914573   1965598   2022272   2081695
##       X1970     X1971     X1972     X1973     X1974     X1975     X1976
## 1     59070     59442     59849     60236     60527     60653     60586
## 2 170257189 175100167 180141148 185376550 190800796 196409937 202205766
## 3  11173654  11475450  11791222  12108963  12412960  12689164  12943093
## 4 120269044 123184308 126218502 129384954 132699537 136173544 139813171
## 5   5890360   6041239   6248965   6497283   6761623   7023994   7279630
## 6   2135479   2187853   2243126   2296752   2350124   2404831   2458526
##       X1977     X1978     X1979     X1980     X1981     X1982     X1983
## 1     60366     60102     59972     60097     60561     61341     62213
## 2 208193045 214368393 220740384 227305945 234058404 240999134 248146290
## 3  13171294  13341199  13411060  13356500  13171679  12882518  12537732
## 4 143615715 147571063 151663853 155882270 160223588 164689764 169279422
## 5   7533814   7790774   8058112   8341290   8640478   8952971   9278104
## 6   2513546   2566266   2617832   2671997   2726056   2784278   2843960
##       X1984     X1985     X1986     X1987     X1988     X1989     X1990
## 1     62826     63024     62645     61838     61072     61033     62152
## 2 255530063 263161451 271050065 279184536 287524258 296024639 304648010
## 3  12204306  11938204  11736177  11604538  11618008  11868873  12412311
## 4 173991851 178826553 183785612 188868567 194070079 199382783 204803865
## 5   9614756   9961993  10320116  10689247  11068051  11454784  11848385
## 6   2904429   2964762   3022635   3083605   3142336   3227943   3286542
##       X1991     X1992     X1993     X1994     X1995     X1996     X1997
## 1     64623     68240     72495     76705     80324     83211     85450
## 2 313394693 322270073 331265579 340379934 349605660 358953595 368440591
## 3  13299016  14485543  15816601  17075728  18110662  18853444  19357126
## 4 210332267 215976366 221754806 227692136 233807627 240114179 246613750
## 5  12248901  12657361  13075044  13503753  13945205  14400722  14871572
## 6   3266790   3247039   3227287   3207536   3187784   3168033   3148281
##       X1998     X1999     X2000     X2001     X2002     X2003     X2004
## 1     87280     89009     90866     92892     94992     97016     98744
## 2 378098393 387977990 398113044 408522129 419223717 430246635 441630149
## 3  19737770  20170847  20779957  21606992  22600774  23680871  24726689
## 4 253302310 260170348 267214544 274433894 281842480 289469530 297353098
## 5  15359600  15866871  16395477  16945753  17519418  18121477  18758138
## 6   3128530   3108778   3089027   3060173   3051010   3039616   3026939
##       X2005     X2006     X2007     X2008     X2009     X2010     X2011
## 1    100028    100830    101226    101362    101452    101665    102050
## 2 453404076 465581372 478166911 491173160 504604672 518468229 532760424
## 3  25654274  26433058  27100542  27722281  28394806  29185511  30117411
## 4 305520588 313985474 322741656 331772330 341050537 350556886 360285439
## 5  19433604  20149905  20905360  21695636  22514275  23356247  24220660
## 6   3011487   2992547   2970017   2947314   2927519   2913021   2905195
##       X2012     X2013     X2014     X2015     X2016     X2017     X2018
## 1    102565    103165    103776    104339    104865    105361    105846
## 2 547482863 562601578 578075373 593871847 609978946 626392880 643090131
## 3  31161378  32269592  33370804  34413603  35383028  36296111  37171922
## 4 370243017 380437896 390882979 401586651 412551299 423769930 435229381
## 5  25107925  26015786  26941773  27884380  28842482  29816769  30809787
## 6   2900401   2895092   2889104   2880703   2876101   2873457   2866376
##       X2019     X2020
## 1    106310    106766
## 2 660046272 677243299
## 3  38041757  38928341
## 4 446911598 458803476
## 5  31825299  32866268
## 6   2854191   2837743

Open the second data file

metadata <- read.csv('country_metadata_2023-11-14 11_59_06.csv')
head(metadata)
##   Country.Code                    Region         IncomeGroup
## 1          ABW Latin America & Caribbean         High income
## 2          AFE                      null                null
## 3          AFG                South Asia          Low income
## 4          AFW                      null                null
## 5          AGO        Sub-Saharan Africa Lower middle income
## 6          ALB     Europe & Central Asia Upper middle income
##                                                                                                                                                                                                                            SpecialNotes
## 1                                                                                                                                                                                                                                  null
## 2                                                                  "26 countries, stretching from the Red Sea in the North to the Cape of Good Hope in the South (https://www.worldbank.org/en/region/afr/eastern-and-southern-africa)"
## 3                                                                                                                                                           Fiscal year end: March 20; reporting period for national accounts data: FY.
## 4 "22 countries, stretching from the westernmost point of Africa, across the equator, and partly along the Atlantic Ocean till the Republic of Congo in the South (https://www.worldbank.org/en/region/afr/western-and-central-africa)"
## 5                                                                                                                                                                                                                                  null
## 6                                                                                                                                                                                                                                  null
##                     TableName
## 1                       Aruba
## 2 Africa Eastern and Southern
## 3                 Afghanistan
## 4  Africa Western and Central
## 5                      Angola
## 6                     Albania

Check out the column names of the fisrt file

colnames(w_data)
##  [1] "Country.Code"   "Indicator.Name" "Indicator.Code" "X1960"         
##  [5] "X1961"          "X1962"          "X1963"          "X1964"         
##  [9] "X1965"          "X1966"          "X1967"          "X1968"         
## [13] "X1969"          "X1970"          "X1971"          "X1972"         
## [17] "X1973"          "X1974"          "X1975"          "X1976"         
## [21] "X1977"          "X1978"          "X1979"          "X1980"         
## [25] "X1981"          "X1982"          "X1983"          "X1984"         
## [29] "X1985"          "X1986"          "X1987"          "X1988"         
## [33] "X1989"          "X1990"          "X1991"          "X1992"         
## [37] "X1993"          "X1994"          "X1995"          "X1996"         
## [41] "X1997"          "X1998"          "X1999"          "X2000"         
## [45] "X2001"          "X2002"          "X2003"          "X2004"         
## [49] "X2005"          "X2006"          "X2007"          "X2008"         
## [53] "X2009"          "X2010"          "X2011"          "X2012"         
## [57] "X2013"          "X2014"          "X2015"          "X2016"         
## [61] "X2017"          "X2018"          "X2019"          "X2020"

Check out the column names of the second file

colnames(metadata)
## [1] "Country.Code" "Region"       "IncomeGroup"  "SpecialNotes" "TableName"

We need to select the columns we want only

new_w_data <- w_data[c(-2,-3)]
new_metadata <- metadata[c(-4)]

We want to join the two data frames using the merge function from the dyplr package

load the dyplr package

library(dplyr)
pop_data <- merge(x = new_metadata, y = new_w_data, by = "Country.Code")

##Rename Column four to Country

colnames(pop_data)[4] <- "CountryName"

Clean the data and Check rows with null values.

any(is.null(pop_data))
## [1] FALSE

The null values are coded as “null” so they do not show up when

applying the function.

We find the rows that contain the value “null” a different way.

null_values <- pop_data%>%filter_all(any_vars(.%in%c('null')))

Remove the rows with the null values in region as these are not countries

pop_data <- subset(pop_data, Region!="null")
head(pop_data)
##   Country.Code                     Region         IncomeGroup
## 1          ABW  Latin America & Caribbean         High income
## 3          AFG                 South Asia          Low income
## 5          AGO         Sub-Saharan Africa Lower middle income
## 6          ALB      Europe & Central Asia Upper middle income
## 7          AND      Europe & Central Asia         High income
## 9          ARE Middle East & North Africa         High income
##            CountryName   X1960   X1961   X1962   X1963   X1964   X1965    X1966
## 1                Aruba   54208   55434   56234   56699   57029   57357    57702
## 3          Afghanistan 8996967 9169406 9351442 9543200 9744772 9956318 10174840
## 5               Angola 5454938 5531451 5608499 5679409 5734995 5770573  5781305
## 6              Albania 1608800 1659800 1711319 1762621 1814135 1864791  1914573
## 7              Andorra   13410   14378   15379   16407   17466   18542    19646
## 9 United Arab Emirates   92417  100801  112112  125130  138049  149855   159979
##      X1967    X1968    X1969    X1970    X1971    X1972    X1973    X1974
## 1    58044    58377    58734    59070    59442    59849    60236    60527
## 3 10399936 10637064 10893772 11173654 11475450 11791222 12108963 12412960
## 5  5774440  5771973  5803677  5890360  6041239  6248965  6497283  6761623
## 6  1965598  2022272  2081695  2135479  2187853  2243126  2296752  2350124
## 7    20760    21886    23053    24275    25571    26885    28232    29515
## 9   169768   182620   203103   234512   277463   330968   394625   467457
##      X1975    X1976    X1977    X1978    X1979    X1980    X1981    X1982
## 1    60653    60586    60366    60102    59972    60097    60561    61341
## 3 12689164 12943093 13171294 13341199 13411060 13356500 13171679 12882518
## 5  7023994  7279630  7533814  7790774  8058112  8341290  8640478  8952971
## 6  2404831  2458526  2513546  2566266  2617832  2671997  2726056  2784278
## 7    30705    31782    32769    33744    34825    36063    37498    39115
## 9   548295   637926   735347   835498   931752  1019507  1096602  1164816
##      X1983    X1984    X1985    X1986    X1987    X1988    X1989    X1990
## 1    62213    62826    63024    62645    61838    61072    61033    62152
## 3 12537732 12204306 11938204 11736177 11604538 11618008 11868873 12412311
## 5  9278104  9614756  9961993 10320116 10689247 11068051 11454784 11848385
## 6  2843960  2904429  2964762  3022635  3083605  3142336  3227943  3286542
## 7    40854    42706    44593    46520    48459    50433    52452    54508
## 9  1228457  1293970  1366165  1446386  1533526  1627068  1725676  1828437
##      X1991    X1992    X1993    X1994    X1995    X1996    X1997    X1998
## 1    64623    68240    72495    76705    80324    83211    85450    87280
## 3 13299016 14485543 15816601 17075728 18110662 18853444 19357126 19737770
## 5 12248901 12657361 13075044 13503753 13945205 14400722 14871572 15359600
## 6  3266790  3247039  3227287  3207536  3187784  3168033  3148281  3128530
## 7    56666    58882    60974    62676    63860    64363    64318    64140
## 9  1937159  2052892  2173135  2294377  2415099  2539121  2671361  2813214
##      X1999    X2000    X2001    X2002    X2003    X2004    X2005    X2006
## 1    89009    90866    92892    94992    97016    98744   100028   100830
## 3 20170847 20779957 21606992 22600774 23680871 24726689 25654274 26433058
## 5 15866871 16395477 16945753 17519418 18121477 18758138 19433604 20149905
## 6  3108778  3089027  3060173  3051010  3039616  3026939  3011487  2992547
## 7    64368    65390    67344    70048    73180    76250    78871    80995
## 9  2966029  3134067  3302722  3478769  3711931  4068577  4588222  5300172
##      X2007    X2008    X2009    X2010    X2011    X2012    X2013    X2014
## 1   101226   101362   101452   101665   102050   102565   103165   103776
## 3 27100542 27722281 28394806 29185511 30117411 31161378 32269592 33370804
## 5 20905360 21695636 22514275 23356247 24220660 25107925 26015786 26941773
## 6  2970017  2947314  2927519  2913021  2905195  2900401  2895092  2889104
## 7    82682    83860    84461    84454    83748    82427    80770    79213
## 9  6168846  7089486  7917368  8549998  8946778  9141598  9197908  9214182
##      X2015    X2016    X2017    X2018    X2019    X2020
## 1   104339   104865   105361   105846   106310   106766
## 3 34413603 35383028 36296111 37171922 38041757 38928341
## 5 27884380 28842482 29816769 30809787 31825299 32866268
## 6  2880703  2876101  2873457  2866376  2854191  2837743
## 7    77993    77295    76997    77008    77146    77265
## 9  9262896  9360975  9487206  9630966  9770526  9890400

Stack the columns of the different years. This can also be achieved using the melt() function

Bind the first four columns

Stack the rest of the columns

stacked_pop_data <- cbind(pop_data[1:4], stack(pop_data[5:65]))

Remove the X on the Years data

stacked_pop_data$ind <- substr(stacked_pop_data$ind, 2, 5)

Change the column heading from ind to Year

colnames(stacked_pop_data)[6] <- 'Year'

Check the structure of the dataframe

str(stacked_pop_data)
## 'data.frame':    13237 obs. of  6 variables:
##  $ Country.Code: chr  "ABW" "AFG" "AGO" "ALB" ...
##  $ Region      : chr  "Latin America & Caribbean" "South Asia" "Sub-Saharan Africa" "Europe & Central Asia" ...
##  $ IncomeGroup : chr  "High income" "Low income" "Lower middle income" "Upper middle income" ...
##  $ CountryName : chr  "Aruba" "Afghanistan" "Angola" "Albania" ...
##  $ values      : num  54208 8996967 5454938 1608800 13410 ...
##  $ Year        : chr  "1960" "1960" "1960" "1960" ...

Change the data type of Year from string to number

stacked_pop_data$Year <- as.numeric(stacked_pop_data$Year)

Create a date column Using lubridate

library(lubridate)
stacked_pop_data$Date <- ymd(stacked_pop_data$Year, truncated = 2L)
head(stacked_pop_data)
##   Country.Code                     Region         IncomeGroup
## 1          ABW  Latin America & Caribbean         High income
## 2          AFG                 South Asia          Low income
## 3          AGO         Sub-Saharan Africa Lower middle income
## 4          ALB      Europe & Central Asia Upper middle income
## 5          AND      Europe & Central Asia         High income
## 6          ARE Middle East & North Africa         High income
##            CountryName  values Year       Date
## 1                Aruba   54208 1960 1960-01-01
## 2          Afghanistan 8996967 1960 1960-01-01
## 3               Angola 5454938 1960 1960-01-01
## 4              Albania 1608800 1960 1960-01-01
## 5              Andorra   13410 1960 1960-01-01
## 6 United Arab Emirates   92417 1960 1960-01-01

Check the structure of the dataframe again

str(stacked_pop_data)
## 'data.frame':    13237 obs. of  7 variables:
##  $ Country.Code: chr  "ABW" "AFG" "AGO" "ALB" ...
##  $ Region      : chr  "Latin America & Caribbean" "South Asia" "Sub-Saharan Africa" "Europe & Central Asia" ...
##  $ IncomeGroup : chr  "High income" "Low income" "Lower middle income" "Upper middle income" ...
##  $ CountryName : chr  "Aruba" "Afghanistan" "Angola" "Albania" ...
##  $ values      : num  54208 8996967 5454938 1608800 13410 ...
##  $ Year        : num  1960 1960 1960 1960 1960 1960 1960 1960 1960 1960 ...
##  $ Date        : Date, format: "1960-01-01" "1960-01-01" ...

We can Output the data frame as csv

write.csv(stacked_pop_data , “C:\Users\Sabelo\Documents\Grad School\GIES\MSBA\Learning\Word Population Project\WorldPop\Population_data.csv”, row.names=FALSE)

Scale the population values for more clear presentation and check for null values

stacked_pop_data$values <- stacked_pop_data$values/1000000
stacked_pop_data <- na.omit(stacked_pop_data)
sum(is.na(stacked_pop_data))
## [1] 0
head(stacked_pop_data)
##   Country.Code                     Region         IncomeGroup
## 1          ABW  Latin America & Caribbean         High income
## 2          AFG                 South Asia          Low income
## 3          AGO         Sub-Saharan Africa Lower middle income
## 4          ALB      Europe & Central Asia Upper middle income
## 5          AND      Europe & Central Asia         High income
## 6          ARE Middle East & North Africa         High income
##            CountryName   values Year       Date
## 1                Aruba 0.054208 1960 1960-01-01
## 2          Afghanistan 8.996967 1960 1960-01-01
## 3               Angola 5.454938 1960 1960-01-01
## 4              Albania 1.608800 1960 1960-01-01
## 5              Andorra 0.013410 1960 1960-01-01
## 6 United Arab Emirates 0.092417 1960 1960-01-01

Now that we cleaned the data, we can do some analysis

load ggplot package for graphing

library(ggplot2)

Plot a line graph to show changes to population for the chosen country

ggplot(filter(stacked_pop_data, CountryName=='United States'), 
  aes(x=Date, y=values)) + 
    geom_line() + 
      ggtitle("United States Population Growth from 1960-2020") +
        xlab("Time (in Years)") +
          ylab("Population in Millions")

Plot a line graph to show changes to population for the chosen countries

ggplot(filter(stacked_pop_data, CountryName %in% c("United States",   "China", "India")), mapping=aes(x=Date, y=values,      group=CountryName, color=CountryName)) + 
  geom_line() + 
    scale_colour_manual("", 
              breaks = c("United States", "China", "India"),
              values = c("blue", "red", "green")) +
  ggtitle("Comparison of Population Changes of United States, China 
                  and India from 1960-2020") +
              xlab("Time (in Years)") +
                ylab("Population in Millions")

How did the population in different parts of the world change over time?

we will use the stat_summary function and specify geom=“line”

ggplot(stacked_pop_data, mapping=aes(x=Date, y=values, color=Region, group=Region)) + 
         stat_summary(fun=sum, geom = "line") +
  ggtitle("Comparison of Population Changes in different
          regions of the world from 1960-2020") +
    xlab("Time (in Years)") +
      ylab("Population in Millions")

Which country or countries have experienced the highest increase/decrease in population over time?

new_d<- stacked_pop_data %>%
  group_by(CountryName) %>%
  mutate(lagged_values = values - lag(values,1))
head(new_d)
## # A tibble: 6 × 8
## # Groups:   CountryName [6]
##   Country.Code Region            Incom…¹ Count…² values  Year Date       lagge…³
##   <chr>        <chr>             <chr>   <chr>    <dbl> <dbl> <date>       <dbl>
## 1 ABW          Latin America & … High i… Aruba   0.0542  1960 1960-01-01      NA
## 2 AFG          South Asia        Low in… Afghan… 9.00    1960 1960-01-01      NA
## 3 AGO          Sub-Saharan Afri… Lower … Angola  5.45    1960 1960-01-01      NA
## 4 ALB          Europe & Central… Upper … Albania 1.61    1960 1960-01-01      NA
## 5 AND          Europe & Central… High i… Andorra 0.0134  1960 1960-01-01      NA
## 6 ARE          Middle East & No… High i… United… 0.0924  1960 1960-01-01      NA
## # … with abbreviated variable names ¹​IncomeGroup, ²​CountryName, ³​lagged_values
new_d %>% 
  filter(Date>="1961-01-01") %>%
  group_by(CountryName) %>%
  summarise(sum_diff=sum(lagged_values, na.rm=T)) %>%
    arrange(desc(sum_diff)) %>%
    slice_head(n=5)
## # A tibble: 5 × 2
##   CountryName sum_diff
##   <chr>          <dbl>
## 1 India           929.
## 2 China           735.
## 3 Indonesia       186.
## 4 Pakistan        176.
## 5 Nigeria         161.
new_d %>% 
  filter(Date>="1961-01-01") %>%
  group_by(CountryName) %>%
  summarise(sum_diff=sum(lagged_values, na.rm=T)) %>%
  arrange(desc(sum_diff)) %>%
  slice_tail(n=5)
## # A tibble: 5 × 2
##   CountryName         sum_diff
##   <chr>                  <dbl>
## 1 St. Kitts and Nevis  0.00199
## 2 Croatia             -0.0930 
## 3 Latvia              -0.219  
## 4 Hungary             -0.234  
## 5 Bulgaria            -0.940

Which country or countries have been experiencing the highest increase/decrease in population in the last five (or ten) years?

new_d %>% 
  filter(Date>="2015-01-01") %>%
  group_by(CountryName) %>%
  summarise(sum_diff=sum(lagged_values, na.rm=T)) %>%
  arrange(desc(sum_diff)) %>%
  slice_head(n=5)
## # A tibble: 5 × 2
##   CountryName sum_diff
##   <chr>          <dbl>
## 1 India           84.4
## 2 China           37.8
## 3 Nigeria         29.7
## 4 Pakistan        25.6
## 5 Indonesia       18.4
new_d %>% 
  filter(Date>="2015-01-01") %>%
  group_by(CountryName) %>%
  summarise(sum_diff=sum(lagged_values, na.rm=T)) %>%
  arrange(desc(sum_diff)) %>%
  slice_tail(n=5)
## # A tibble: 5 × 2
##   CountryName            sum_diff
##   <chr>                     <dbl>
## 1 "Ukraine"                 -1.14
## 2 "Syrian Arab Republic"    -1.21
## 3 "Italy"                   -1.24
## 4 "Japan"                   -1.44
## 5 "\"Venezuela, RB\""       -1.61

How many people were born in your country (or any other country) during your birth year?

new_d %>%
  filter(Date=="1994-01-01") %>%
  filter(CountryName=="Eswatini") %>%
  group_by(CountryName) %>%
  summarise(num_born=mean(lagged_values, na.rm=T)*1000000)
## # A tibble: 1 × 2
##   CountryName num_born
##   <chr>          <dbl>
## 1 Eswatini      19916.

It was realized that there are some null coded entries in the income group column. Remove null entries

stacked_pop_data <- subset(stacked_pop_data, IncomeGroup!="null")

How does income group affect a country’s population growth?

ggplot(stacked_pop_data, mapping=aes(x=Date, y=values, color=IncomeGroup, group=IncomeGroup)) + 
  stat_summary(fun=sum, geom = "line") +
  ggtitle("Comparison of Population Changes in different country
          income groups from 1960-2020") +
  xlab("Time (in Years)") +
  ylab("Population in Millions")