# Displaying an introduction to the dataset:

library(readr)
## Warning: package 'readr' was built under R version 4.3.3
wpdata<-read.csv("wpdata.csv")
str(wpdata)
## 'data.frame':    234 obs. of  17 variables:
##  $ Rank                       : int  36 138 34 213 203 42 224 201 33 140 ...
##  $ CCA3                       : chr  "AFG" "ALB" "DZA" "ASM" ...
##  $ Country.Territory          : chr  "Afghanistan" "Albania" "Algeria" "American Samoa" ...
##  $ Capital                    : chr  "Kabul" "Tirana" "Algiers" "Pago Pago" ...
##  $ Continent                  : chr  "Asia" "Europe" "Africa" "Oceania" ...
##  $ X2022.Population           : int  41128771 2842321 44903225 44273 79824 35588987 15857 93763 45510318 2780469 ...
##  $ X2020.Population           : int  38972230 2866849 43451666 46189 77700 33428485 15585 92664 45036032 2805608 ...
##  $ X2015.Population           : int  33753499 2882481 39543154 51368 71746 28127721 14525 89941 43257065 2878595 ...
##  $ X2010.Population           : int  28189672 2913399 35856344 54849 71519 23364185 13172 85695 41100123 2946293 ...
##  $ X2000.Population           : int  19542982 3182021 30774621 58230 66097 16394062 11047 75055 37070774 3168523 ...
##  $ X1990.Population           : int  10694796 3295066 25518074 47818 53569 11828638 8316 63328 32637657 3556539 ...
##  $ X1980.Population           : int  12486631 2941651 18739378 32886 35611 8330047 6560 64888 28024803 3135123 ...
##  $ X1970.Population           : int  10752971 2324731 13795915 27075 19860 6029700 6283 64516 23842803 2534377 ...
##  $ Area..kmÂ..                : int  652230 28748 2381741 199 468 1246700 91 442 2780400 29743 ...
##  $ Density..per.kmÂ..         : num  63.1 98.9 18.9 222.5 170.6 ...
##  $ Growth.Rate                : num  1.026 0.996 1.016 0.983 1.01 ...
##  $ World.Population.Percentage: num  0.52 0.04 0.56 0 0 0.45 0 0 0.57 0.03 ...
#1).What is the total population of the world according to the dataset?

#In 1970:
wp_1970<-sum(wpdata$X1970.Population)
#In 2000:
wp_2000<-sum(wpdata$X2000.Population)
#In 2022:
wp_2022<-sum(wpdata$X2022.Population)

cat("World Population in 1970: ",wp_1970, " ")
## World Population in 1970:  3694136661
cat("\nWorld Population in 2000: ",wp_2000,  " ", "Change(1970-2000):",wp_2000-wp_1970)
## 
## World Population in 2000:  6147055703   Change(1970-2000): 2452919042
cat("\nWorld Population in 2022: ",wp_2022,  " ", "Change(2000-2022):",wp_2022-wp_2000)
## 
## World Population in 2022:  7973413042   Change(2000-2022): 1826357339
barplot(c(wp_1970, wp_2000, wp_2022), xlab = "Year", ylab = "Population", main = "Population over year", names.arg = c("1970", "2000", "2022"), col = c("red", "green", "blue"))

#2).How much Population increased between 1970 and 2022?

#Making a new column in of difference in world population bewtween 1970-2022 of each country:
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
wpdata<-mutate(wpdata, DifferenceX2022_X1970=X2022.Population-X1970.Population)

#Percentage increased in World Population Country wise:
wpdata<-mutate(wpdata, PerecIncreased=(DifferenceX2022_X1970/X1970.Population)*100)
pop_diff<-wpdata%>%select(Country.Territory, X2022.Population, X1970.Population, PerecIncreased)%>%slice_head(n=10)
pop_diff
##      Country.Territory X2022.Population X1970.Population PerecIncreased
## 1          Afghanistan         41128771         10752971     282.487510
## 2              Albania          2842321          2324731      22.264511
## 3              Algeria         44903225         13795915     225.482036
## 4       American Samoa            44273            27075      63.519852
## 5              Andorra            79824            19860     301.933535
## 6               Angola         35588987          6029700     490.228154
## 7             Anguilla            15857             6283     152.379437
## 8  Antigua and Barbuda            93763            64516      45.332941
## 9            Argentina         45510318         23842803      90.876542
## 10             Armenia          2780469          2534377       9.710158
#Barplotting:
barplot(pop_diff$PerecIncreased, col = c("red", "green", "blue", "black","orange","maroon","grey","pink","navy","skyblue","brown"), names.arg = pop_diff$Country.Territor, las=2, xlab = "Countries", ylab = "% increase in Population", main = "Percentage increased in World Population in Terms of Countries")

#3).Identifying countries with declining populations?

library(dplyr)
dec_pop<-wpdata%>%select(Country.Territory,PerecIncreased)%>%filter(PerecIncreased<0)
dec_pop
##         Country.Territory PerecIncreased
## 1  Bosnia and Herzegovina     -15.254244
## 2                Bulgaria     -20.983426
## 3            Cook Islands     -16.897899
## 4                 Croatia     -10.289723
## 5                 Estonia      -2.638548
## 6                 Georgia     -21.998902
## 7                 Hungary      -3.374170
## 8                  Latvia     -22.806366
## 9               Lithuania     -14.332428
## 10                Moldova     -11.806184
## 11             Montserrat     -61.497983
## 12                   Niue     -62.700096
## 13                Romania      -1.321869
## 14                Ukraine     -16.026847
## 15           Vatican City     -32.180851
#Most decrease in Population:
most_dec<-min(dec_pop$PerecIncreased)
cat("Most decrease in Population: ",most_dec)
## Most decrease in Population:  -62.7001
#Pie Chart:
pie(abs(dec_pop$PerecIncreased), labels = dec_pop$Country.Territory, main = "Decrease in Population", col = rainbow(length(dec_pop$Country.Territory)))

#4).The total population for each continent in year 2022:

library(dplyr)
total_population_by_continent<-wpdata%>%group_by(Continent)%>%summarize(Total_Population=sum(X2022.Population))
total_population_by_continent
## # A tibble: 6 × 2
##   Continent     Total_Population
##   <chr>                    <dbl>
## 1 Africa              1426730932
## 2 Asia                4721383274
## 3 Europe               743147538
## 4 North America        600296136
## 5 Oceania               45038554
## 6 South America        436816608
#Barplotting:
barplot(total_population_by_continent$Total_Population, names.arg = total_population_by_continent$Continent, las = 2, xlab = "Continent", ylab = "Population", main = "Total Population in Each Continent", col = c("yellow","maroon","orange","brown","red","blue"))

#5).Top 10 populated countries in 2022:
library(dplyr)
wpdata1<-wpdata%>%select(Country.Territory, Rank,X2022.Population)%>%arrange(desc(X2022.Population))
wpdata1<-wpdata1%>%slice_head(n=10)
wpdata1
##    Country.Territory Rank X2022.Population
## 1              China    1       1425887337
## 2              India    2       1417173173
## 3      United States    3        338289857
## 4          Indonesia    4        275501339
## 5           Pakistan    5        235824862
## 6            Nigeria    6        218541212
## 7             Brazil    7        215313498
## 8         Bangladesh    8        171186372
## 9             Russia    9        144713314
## 10            Mexico   10        127504125
#Barplotting:
barplot(wpdata1$X2022.Population, names.arg = wpdata1$Country.Territory, xlab = "Countries", ylab = "Population", main = "Top 10 populated Countries", col = c("red", "green", "blue", "black","orange","maroon","grey","pink","navy","skyblue","brown"))

#6).Average Density of Population in World?
avg_density<-mean(wpdata$Density..per.kmÂ..)
cat("Average density of Poulation: ",avg_density)
## Average density of Poulation:  452.127
#7).What is the growth rate of population of \n top 10 populated countries:
library(dplyr)
wpdata2<-wpdata%>%select(Country.Territory, Growth.Rate,X2022.Population)%>%arrange(desc(X2022.Population))%>%slice_head(n=10)
wpdata2
##    Country.Territory Growth.Rate X2022.Population
## 1              China      1.0000       1425887337
## 2              India      1.0068       1417173173
## 3      United States      1.0038        338289857
## 4          Indonesia      1.0064        275501339
## 5           Pakistan      1.0191        235824862
## 6            Nigeria      1.0241        218541212
## 7             Brazil      1.0046        215313498
## 8         Bangladesh      1.0108        171186372
## 9             Russia      0.9973        144713314
## 10            Mexico      1.0063        127504125
#Country with highest Growth Rate:
max(wpdata2$Growth.Rate)
## [1] 1.0241
#Barplotting:
barplot(wpdata2$Growth.Rate, names.arg = wpdata2$Country.Territory, xlab = "Countries", ylab = "Growth Rate", col = c("red", "green", "blue", "black","orange","maroon","grey","pink","navy","skyblue","brown"), main = "Growth Rate of Population.", las = 2)

#8).Which country has most population but Area of that country is small in top 10 populated country:
wpdata3<-wpdata%>%select(Country.Territory, Density..per.kmÂ..,Area..kmÂ..,X2022.Population)%>%arrange(desc(X2022.Population))%>%slice_head(n=10)
wpdata3
##    Country.Territory Density..per.kmÂ.. Area..kmÂ.. X2022.Population
## 1              China           146.8933     9706961       1425887337
## 2              India           431.0675     3287590       1417173173
## 3      United States            36.0935     9372610        338289857
## 4          Indonesia           144.6529     1904569        275501339
## 5           Pakistan           267.4018      881912        235824862
## 6            Nigeria           236.5759      923768        218541212
## 7             Brazil            25.2841     8515767        215313498
## 8         Bangladesh          1160.0350      147570        171186372
## 9             Russia             8.4636    17098242        144713314
## 10            Mexico            64.9082     1964375        127504125
min(wpdata3$Area..kmÂ..)
## [1] 147570
#Barplotting:
barplot(wpdata3$X2022.Population, main = "Population in 2022", xlab = "Countries", ylab = "Population", names.arg = wpdata3$Country.Territory, las = 2,col = "orange")

barplot(wpdata3$Area..kmÂ.., main = "Area of The Country", xlab = "Country", ylab = "Area", las = 2, names.arg = wpdata3$Country.Territory, col = "red", density = 60)

#9).Change in Rank of the countries in top 10 populated countries in 1970 and 2022:
library(dplyr)
wpdata4<-wpdata%>%select(Country.Territory, X1970.Population)%>%arrange(desc(X1970.Population))%>%slice_head(n=10)
wpdata4
##    Country.Territory X1970.Population
## 1              China        822534450
## 2              India        557501301
## 3      United States        200328340
## 4             Russia        130093010
## 5          Indonesia        115228394
## 6              Japan        105416839
## 7             Brazil         96369875
## 8            Germany         78294583
## 9         Bangladesh         67541860
## 10          Pakistan         59290872
wpdata1
##    Country.Territory Rank X2022.Population
## 1              China    1       1425887337
## 2              India    2       1417173173
## 3      United States    3        338289857
## 4          Indonesia    4        275501339
## 5           Pakistan    5        235824862
## 6            Nigeria    6        218541212
## 7             Brazil    7        215313498
## 8         Bangladesh    8        171186372
## 9             Russia    9        144713314
## 10            Mexico   10        127504125
#10).Which countries are recorded highest increase in population:
max_perc<-wpdata%>%select(Country.Territory, PerecIncreased)%>%arrange(desc(PerecIncreased))%>%slice_head(n=10)
max_perc
##           Country.Territory PerecIncreased
## 1      United Arab Emirates      3067.2713
## 2                     Qatar      2183.8662
## 3                   Mayotte       821.6319
## 4  Turks and Caicos Islands       706.7608
## 5                  Djibouti       676.3241
## 6            Western Sahara       654.1947
## 7                    Jordan       624.6730
## 8              Sint Maarten       605.6709
## 9                      Oman       582.3238
## 10                  Bahrain       561.5142
#Barplotting:
barplot(max_perc$PerecIncreased, names.arg = max_perc$Country.Territory, xlab = "Country", ylab = "% Increased", main = "Recorded highest increase in population \n Between 1970-2022", col = c("red", "green", "blue", "black","orange","maroon","grey","pink","navy","skyblue","brown"), las = 2)

#11).How much percentage of world population held by top 10 populated countries:
library(dplyr)
most_wpperc<-wpdata%>%select(Country.Territory, World.Population.Percentage, Area..kmÂ..)%>%arrange(desc(World.Population.Percentage))%>%slice_head(n=10)
most_wpperc
##    Country.Territory World.Population.Percentage Area..kmÂ..
## 1              China                       17.88     9706961
## 2              India                       17.77     3287590
## 3      United States                        4.24     9372610
## 4          Indonesia                        3.45     1904569
## 5           Pakistan                        2.96      881912
## 6            Nigeria                        2.74      923768
## 7             Brazil                        2.70     8515767
## 8         Bangladesh                        2.15      147570
## 9             Russia                        1.81    17098242
## 10            Mexico                        1.60     1964375
#Pie Chart:
pie(most_wpperc$World.Population.Percentage, labels = abs(most_wpperc$World.Population.Percentage), main = "Percentage of world population held by \n top 10 populated countries.")