#make use about the workind directory

getwd()
## [1] "C:/Users/Isabelle.Izabayo/Desktop/PYTHONCLASSES"
setwd("C:/Users/Isabelle.Izabayo/Desktop/PYTHONCLASSES")

##4.1.load data to R,HW1

wp<- read.csv("world_population.csv")
CO2<-read.csv("CO2_emission.csv")
head(CO2)
##           Country.Name country_code                     Region
## 1                Aruba          ABW  Latin America & Caribbean
## 2          Afghanistan          AFG                 South Asia
## 3               Angola          AGO         Sub-Saharan Africa
## 4              Albania          ALB      Europe & Central Asia
## 5              Andorra          AND      Europe & Central Asia
## 6 United Arab Emirates          ARE Middle East & North Africa
##                           Indicator.Name      X1990      X1991       X1992
## 1 CO2 emissions (metric tons per capita)         NA         NA          NA
## 2 CO2 emissions (metric tons per capita)  0.1917451  0.1676816  0.09595774
## 3 CO2 emissions (metric tons per capita)  0.5536620  0.5445386  0.54355722
## 4 CO2 emissions (metric tons per capita)  1.8195416  1.2428102  0.68369983
## 5 CO2 emissions (metric tons per capita)  7.5218317  7.2353792  6.96307870
## 6 CO2 emissions (metric tons per capita) 30.1951886 31.7784962 29.08092584
##         X1993       X1994       X1995       X1996       X1997       X1998
## 1          NA          NA          NA          NA          NA          NA
## 2  0.08472111  0.07554583  0.06846796  0.06258803  0.05682662  0.05269086
## 3  0.70898423  0.83680440  0.91214149  1.07216847  1.08663697  1.09182531
## 4  0.63830704  0.64535519  0.60543625  0.61236736  0.46692147  0.57215370
## 5  6.72417752  6.54157891  6.73347949  6.99159455  7.30744115  7.63953851
## 6 29.27567777 30.84933296 31.12501806 30.92802588 30.48633262 29.66358052
##         X1999      X2000       X2001       X2002       X2003       X2004
## 1          NA         NA          NA          NA          NA          NA
## 2  0.04015697  0.0365737  0.03378536  0.04557366  0.05151838  0.04165539
## 3  1.10985966  0.9880774  0.94182891  0.89557767  0.92486944  0.93026295
## 4  0.95535931  1.0262131  1.05549588  1.23237878  1.33898498  1.40405869
## 5  7.92319165  7.9522863  7.72154906  7.56623988  7.24241557  7.34426233
## 6 28.88710798 27.0351591 29.43026994 28.50146173 27.96926982 27.03893822
##         X2005       X2006       X2007      X2008      X2009      X2010
## 1          NA          NA          NA         NA         NA         NA
## 2  0.06041878  0.06658329  0.06531235  0.1284166  0.1718624  0.2436140
## 3  0.81353929  0.82184008  0.81175351  0.8886580  0.9394040  0.9761842
## 4  1.33820940  1.33999574  1.39393137  1.3843112  1.4414936  1.5276237
## 5  7.35378001  6.79054277  6.53104692  6.4393039  6.1566875  6.1571978
## 6 25.38238104 22.93510429 21.37028576 22.0114692 19.8323489 19.0397698
##        X2011      X2012      X2013      X2014      X2015      X2016      X2017
## 1         NA         NA         NA         NA         NA         NA         NA
## 2  0.2965062  0.2592953  0.1856237  0.1462356  0.1728967  0.1497893  0.1316946
## 3  0.9855223  0.9506959  1.0362939  1.0997791  1.1350441  1.0318113  0.8133007
## 4  1.6694232  1.5032405  1.5336300  1.6683374  1.6037751  1.5576644  1.7887861
## 5  5.8508861  5.9446542  5.9428004  5.8071277  6.0261818  6.0806003  6.1041339
## 6 18.5094574 19.2078011 20.0556476 20.0516980 21.0776420 21.4806686 20.7690223
##        X2018      X2019    X2019.1
## 1         NA         NA         NA
## 2  0.1632953  0.1598244  0.1598244
## 3  0.7776749  0.7921371  0.7921371
## 4  1.7827389  1.6922483  1.6922483
## 5  6.3629754  6.4812174  6.4812174
## 6 18.3906781 19.3295633 19.3295633
##4.2.Exploratory data analysis
variable.names(wp)
##  [1] "Rank"                        "CCA3"                       
##  [3] "Country.Territory"           "Capital"                    
##  [5] "Continent"                   "X2022.Population"           
##  [7] "X2020.Population"            "X2015.Population"           
##  [9] "X2010.Population"            "X2000.Population"           
## [11] "X1990.Population"            "X1980.Population"           
## [13] "X1970.Population"            "Area..km.."                 
## [15] "Density..per.km.."           "Growth.Rate"                
## [17] "World.Population.Percentage"
head(wp)
##   Rank CCA3 Country.Territory          Capital Continent X2022.Population
## 1   36  AFG       Afghanistan            Kabul      Asia         41128771
## 2  138  ALB           Albania           Tirana    Europe          2842321
## 3   34  DZA           Algeria          Algiers    Africa         44903225
## 4  213  ASM    American Samoa        Pago Pago   Oceania            44273
## 5  203  AND           Andorra Andorra la Vella    Europe            79824
## 6   42  AGO            Angola           Luanda    Africa         35588987
##   X2020.Population X2015.Population X2010.Population X2000.Population
## 1         38972230         33753499         28189672         19542982
## 2          2866849          2882481          2913399          3182021
## 3         43451666         39543154         35856344         30774621
## 4            46189            51368            54849            58230
## 5            77700            71746            71519            66097
## 6         33428485         28127721         23364185         16394062
##   X1990.Population X1980.Population X1970.Population Area..km..
## 1         10694796         12486631         10752971     652230
## 2          3295066          2941651          2324731      28748
## 3         25518074         18739378         13795915    2381741
## 4            47818            32886            27075        199
## 5            53569            35611            19860        468
## 6         11828638          8330047          6029700    1246700
##   Density..per.km.. Growth.Rate World.Population.Percentage
## 1           63.0587      1.0257                        0.52
## 2           98.8702      0.9957                        0.04
## 3           18.8531      1.0164                        0.56
## 4          222.4774      0.9831                        0.00
## 5          170.5641      1.0100                        0.00
## 6           28.5466      1.0315                        0.45
tail(wp,n=10)
##     Rank CCA3 Country.Territory      Capital     Continent X2022.Population
## 225   43  UZB        Uzbekistan     Tashkent          Asia         34627652
## 226  181  VUT           Vanuatu    Port-Vila       Oceania           326740
## 227  234  VAT      Vatican City Vatican City        Europe              510
## 228   51  VEN         Venezuela      Caracas South America         28301696
## 229   16  VNM           Vietnam        Hanoi          Asia         98186856
## 230  226  WLF Wallis and Futuna     Mata-Utu       Oceania            11572
## 231  172  ESH    Western Sahara     El Aaiún        Africa           575986
## 232   46  YEM             Yemen        Sanaa          Asia         33696614
## 233   63  ZMB            Zambia       Lusaka        Africa         20017675
## 234   74  ZWE          Zimbabwe       Harare        Africa         16320537
##     X2020.Population X2015.Population X2010.Population X2000.Population
## 225         33526656         30949417         28614227         24925554
## 226           311685           276438           245453           192074
## 227              520              564              596              651
## 228         28490453         30529716         28715022         24427729
## 229         96648685         92191398         87411012         79001142
## 230            11655            12182            13142            14723
## 231           556048           491824           413296           270375
## 232         32284046         28516545         24743946         18628700
## 233         18927715         16248230         13792086          9891136
## 234         15669666         14154937         12839771         11834676
##     X1990.Population X1980.Population X1970.Population Area..km..
## 225         20579100         15947129         12011361     447400
## 226           150882           118156            87019      12189
## 227              700              733              752          1
## 228         19750579         15210443         11355475     916445
## 229         66912613         52968270         41928849     331212
## 230            13454            11315             9377        142
## 231           178529           116775            76371     266000
## 232         13375121          9204938          6843607     527968
## 233          7686401          5720438          4281671     752612
## 234         10113893          7049926          5202918     390757
##     Density..per.km.. Growth.Rate World.Population.Percentage
## 225           77.3975      1.0160                        0.43
## 226           26.8061      1.0238                        0.00
## 227          510.0000      0.9980                        0.00
## 228           30.8820      1.0036                        0.35
## 229          296.4472      1.0074                        1.23
## 230           81.4930      0.9953                        0.00
## 231            2.1654      1.0184                        0.01
## 232           63.8232      1.0217                        0.42
## 233           26.5976      1.0280                        0.25
## 234           41.7665      1.0204                        0.20
length(wp)
## [1] 17
##data type
str(wp)
## '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 ...
##shape of dataset
dim(wp)
## [1] 234  17
##check duplicates
duplicated(wp)
##   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
##  [97] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [109] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [121] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [133] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [145] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [157] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [169] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [181] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [193] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [205] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [217] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [229] FALSE FALSE FALSE FALSE FALSE FALSE
##check missing values
colSums(is.na(wp))
##                        Rank                        CCA3 
##                           0                           0 
##           Country.Territory                     Capital 
##                           0                           0 
##                   Continent            X2022.Population 
##                           0                           0 
##            X2020.Population            X2015.Population 
##                           0                           0 
##            X2010.Population            X2000.Population 
##                           0                           0 
##            X1990.Population            X1980.Population 
##                           0                           0 
##            X1970.Population                  Area..km.. 
##                           0                           0 
##           Density..per.km..                 Growth.Rate 
##                           0                           0 
## World.Population.Percentage 
##                           0
sum(is.na(wp))
## [1] 0

#HW4:use of sapply():is a function used to apply another function to each element of a list or vector ##apply family (apply(), lapply(), sapply()):avoid writting loops #lapply():always return list #sapply():always return simplified result #use of split():used to divide dataset into smaller

continents_list <- split(wp, wp$Continent)
continents_list
## $Africa
##     Rank CCA3        Country.Territory      Capital Continent X2022.Population
## 3     34  DZA                  Algeria      Algiers    Africa         44903225
## 6     42  AGO                   Angola       Luanda    Africa         35588987
## 22    77  BEN                    Benin   Porto-Novo    Africa         13352864
## 27   144  BWA                 Botswana     Gaborone    Africa          2630296
## 32    58  BFA             Burkina Faso  Ouagadougou    Africa         22673762
## 33    78  BDI                  Burundi    Bujumbura    Africa         12889576
## 35    53  CMR                 Cameroon      Yaounde    Africa         27914536
## 37   171  CPV               Cape Verde        Praia    Africa           593149
## 39   117  CAF Central African Republic       Bangui    Africa          5579144
## 40    69  TCD                     Chad    N'Djamena    Africa         17723315
## 44   163  COM                  Comoros       Moroni    Africa           836774
## 53   160  DJI                 Djibouti     Djibouti    Africa          1120849
## 56    15  COD                 DR Congo     Kinshasa    Africa         99010212
## 58    14  EGY                    Egypt        Cairo    Africa        110990103
## 60   152  GNQ        Equatorial Guinea       Malabo    Africa          1674908
## 61   132  ERI                  Eritrea       Asmara    Africa          3684032
## 63   159  SWZ                 Eswatini      Mbabane    Africa          1201670
## 64    12  ETH                 Ethiopia  Addis Ababa    Africa        123379924
## 72   146  GAB                    Gabon   Libreville    Africa          2388992
## 73   142  GMB                   Gambia       Banjul    Africa          2705992
## 76    47  GHA                    Ghana        Accra    Africa         33475870
## 85    75  GIN                   Guinea      Conakry    Africa         13859341
## 86   149  GNB            Guinea-Bissau       Bissau    Africa          2105566
## 101   52  CIV              Ivory Coast Yamoussoukro    Africa         28160542
## 107   27  KEN                    Kenya      Nairobi    Africa         54027487
## 114  147  LSO                  Lesotho       Maseru    Africa          2305825
## 115  121  LBR                  Liberia     Monrovia    Africa          5302681
## 116  107  LBY                    Libya      Tripoli    Africa          6812341
## 121   50  MDG               Madagascar Antananarivo    Africa         29611714
## 122   62  MWI                   Malawi     Lilongwe    Africa         20405317
## 125   59  MLI                     Mali       Bamako    Africa         22593590
## 129  126  MRT               Mauritania   Nouakchott    Africa          4736139
## 130  157  MUS                Mauritius   Port Louis    Africa          1299469
## 131  182  MYT                  Mayotte    Mamoudzou    Africa           326101
## 139   40  MAR                  Morocco        Rabat    Africa         37457971
## 140   48  MOZ               Mozambique       Maputo    Africa         32969517
## 142  145  NAM                  Namibia     Windhoek    Africa          2567012
## 149   54  NER                    Niger       Niamey    Africa         26207977
## 150    6  NGA                  Nigeria        Abuja    Africa        218541212
## 169  114  COG    Republic of the Congo  Brazzaville    Africa          5970424
## 170  161  REU                  Reunion  Saint-Denis    Africa           974052
## 173   76  RWA                   Rwanda       Kigali    Africa         13776698
## 182  187  STP    Sao Tome and Principe     São Tomé    Africa           227380
## 184   72  SEN                  Senegal        Dakar    Africa         17316449
## 186  196  SYC               Seychelles     Victoria    Africa           107118
## 187  102  SLE             Sierra Leone     Freetown    Africa          8605718
## 193   70  SOM                  Somalia    Mogadishu    Africa         17597511
## 194   24  ZAF             South Africa     Pretoria    Africa         59893885
## 196   86  SSD              South Sudan         Juba    Africa         10913164
## 199   32  SDN                    Sudan     Khartoum    Africa         46874204
## 206   22  TZA                 Tanzania       Dodoma    Africa         65497748
## 209  100  TGO                     Togo         Lomé    Africa          8848699
## 213   79  TUN                  Tunisia        Tunis    Africa         12356117
## 218   31  UGA                   Uganda      Kampala    Africa         47249585
## 231  172  ESH           Western Sahara     El Aaiún    Africa           575986
## 233   63  ZMB                   Zambia       Lusaka    Africa         20017675
## 234   74  ZWE                 Zimbabwe       Harare    Africa         16320537
##     X2020.Population X2015.Population X2010.Population X2000.Population
## 3           43451666         39543154         35856344         30774621
## 6           33428485         28127721         23364185         16394062
## 22          12643123         10932783          9445710          6998023
## 27           2546402          2305171          2091664          1726985
## 32          21522626         18718019         16116845         11882888
## 33          12220227         10727148          9126605          6307659
## 35          26491087         23012646         19878036         15091594
## 37            582640           552166           521212           458251
## 39           5343020          4819333          4660067          3759170
## 40          16644701         14140274         11894727          8259137
## 44            806166           730216           656024           536758
## 53           1090156          1006259           919199           742033
## 56          92853164         78656904         66391257         48616317
## 58         107465134         97723799         87252413         71371371
## 60           1596049          1346973          1094524           684977
## 61           3555868          3340006          3147727          2392880
## 63           1180655          1133936          1099920          1030496
## 64         117190911        102471895         89237791         67031867
## 72           2292573          2028517          1711105          1272935
## 73           2573995          2253133          1937275          1437539
## 76          32180401         28870939         25574719         19665502
## 85          13205153         11625998         10270728          8336967
## 86           2015828          1788919          1567220          1230849
## 101         26811790         23596741         21120042         16799670
## 107         51985780         46851488         41517895         30851606
## 114          2254100          2118521          2022747          1998630
## 115          5087584          4612329          4019956          2895224
## 116          6653942          6192235          6491988          5154790
## 121         28225177         24850912         21731053         16216431
## 122         19377061         16938942         14718422         11229387
## 125         21224040         18112907         15529181         11239101
## 129          4498604          3946220          3419461          2695003
## 130          1297828          1293153          1283330          1215930
## 131           305587           249545           211786           159215
## 139         36688772         34680458         32464865         28554415
## 140         31178239         26843246         23073723         17768505
## 142          2489098          2282704          2099271          1819141
## 149         24333639         20128124         16647543         11622665
## 150        208327405        183995785        160952853        122851984
## 169          5702174          5064386          4437884          3134030
## 170           957822           922495           890130           785424
## 173         13146362         11642959         10309031          8109989
## 182           218641           201124           182138           143714
## 184         16436119         14356181         12530121          9704287
## 186           105530            99240            92409            80060
## 187          8233969          7314773          6436698          4584067
## 193         16537016         13763906         12026649          8721465
## 194         58801927         55876504         51784921         46813266
## 196         10606227         11194299          9714419          6114440
## 199         44440486         38171178         33739933         26298773
## 206         61704518         52542823         45110527         34463704
## 209          8442580          7473229          6571855          5008035
## 213         12161723         11557779         10895063          9893316
## 218         44404611         37477356         32341728         24020697
## 231           556048           491824           413296           270375
## 233         18927715         16248230         13792086          9891136
## 234         15669666         14154937         12839771         11834676
##     X1990.Population X1980.Population X1970.Population Area..km..
## 3           25518074         18739378         13795915    2381741
## 6           11828638          8330047          6029700    1246700
## 22           5133419          3833939          3023443     112622
## 27           1341474           938578           592244     582000
## 32           9131361          6932967          5611666     272967
## 33           5483793          4312834          3497834      27834
## 35          11430520          8519891          6452787     475442
## 37            364563           317234           287262       4033
## 39           2809221          2415276          2067356     622984
## 40           5827069          4408230          3667394    1284000
## 44            431119           328328           242351       1862
## 53            577173           324121           144379      23200
## 56          35987541         26708686         20151733    2344858
## 58          57214630         43748556         34781986    1002450
## 60            465549           282509           316955      28051
## 61           2149960          1657982          1272748     117600
## 63            854011           598564           442865      17364
## 64          47878073         34945469         28308246    1104300
## 72            983028           749078           597192     267668
## 73           1040616           718586           528731      10689
## 76          15446982         11865246          8861895     238533
## 85           6354145          4972609          4222374     245857
## 86            973551           831462           591663      36125
## 101         11910540          8303809          5477086     322463
## 107         23162269         16187124         11473087     580367
## 114          1798997          1407672          1023481      30355
## 115          2209731          1932169          1463563     111369
## 116          4236983          2962720          1909177    1759540
## 121         11882762          8948162          6639751     587041
## 122          9539665          6267369          4625141     118484
## 125          8945026          7372581          6153587    1240192
## 129          2006027          1506694          1122198    1030700
## 130          1090290           954865           830115       2040
## 131            92659            52233            35383        374
## 139         24570814         19678444         15274351     446550
## 140         13303459         11413587          8411676     801590
## 142          1369011           975994           754467     825615
## 149          8370647          6173177          4669708    1267000
## 150         95214257         72951439         55569264     923768
## 169          2385435          1829256          1396989     342000
## 170           658992           551674           473925       2511
## 173          7319962          5247532          3896367      26338
## 182           120343            97210            77583        964
## 184          7536001          5703869          4367744     196722
## 186            71057            65290            54379        452
## 187          4325388          3367477          2778557      71740
## 193          6999096          5892224          3720977     637657
## 194         39877570         29463549         22368306    1221037
## 196          4750817          4192011          3342410     619745
## 199         21090886         16673586         11305206    1886068
## 206         26206012         19297659         13618192     945087
## 209          3875947          2838110          2197383      56785
## 213          8440023          6578156          5047404     163610
## 218         17586630         13284026         10317212     241550
## 231           178529           116775            76371     266000
## 233          7686401          5720438          4281671     752612
## 234         10113893          7049926          5202918     390757
##     Density..per.km.. Growth.Rate World.Population.Percentage
## 3             18.8531      1.0164                        0.56
## 6             28.5466      1.0315                        0.45
## 22           118.5635      1.0274                        0.17
## 27             4.5194      1.0162                        0.03
## 32            83.0641      1.0259                        0.28
## 33           463.0874      1.0270                        0.16
## 35            58.7128      1.0263                        0.35
## 37           147.0739      1.0089                        0.01
## 39             8.9555      1.0224                        0.07
## 40            13.8032      1.0316                        0.22
## 44           449.3953      1.0184                        0.01
## 53            48.3125      1.0138                        0.01
## 56            42.2244      1.0325                        1.24
## 58           110.7188      1.0158                        1.39
## 60            59.7094      1.0247                        0.02
## 61            31.3268      1.0176                        0.05
## 63            69.2047      1.0079                        0.02
## 64           111.7268      1.0257                        1.55
## 72             8.9252      1.0204                        0.03
## 73           253.1567      1.0250                        0.03
## 76           140.3406      1.0196                        0.42
## 85            56.3716      1.0242                        0.17
## 86            58.2856      1.0218                        0.03
## 101           87.3295      1.0248                        0.35
## 107           93.0919      1.0193                        0.68
## 114           75.9620      1.0107                        0.03
## 115           47.6136      1.0210                        0.07
## 116            3.8717      1.0114                        0.09
## 121           50.4423      1.0241                        0.37
## 122          172.2200      1.0259                        0.26
## 125           18.2178      1.0314                        0.28
## 129            4.5951      1.0263                        0.06
## 130          636.9946      1.0004                        0.02
## 131          871.9278      1.0319                        0.00
## 139           83.8830      1.0103                        0.47
## 140           41.1302      1.0278                        0.41
## 142            3.1092      1.0146                        0.03
## 149           20.6851      1.0378                        0.33
## 150          236.5759      1.0241                        2.74
## 169           17.4574      1.0231                        0.07
## 170          387.9140      1.0082                        0.01
## 173          523.0731      1.0234                        0.17
## 182          235.8714      1.0192                        0.00
## 184           88.0250      1.0261                        0.22
## 186          236.9867      1.0061                        0.00
## 187          119.9570      1.0220                        0.11
## 193           27.5971      1.0312                        0.22
## 194           49.0517      1.0084                        0.75
## 196           17.6091      1.0153                        0.14
## 199           24.8529      1.0267                        0.59
## 206           69.3034      1.0300                        0.82
## 209          155.8281      1.0236                        0.11
## 213           75.5218      1.0076                        0.15
## 218          195.6100      1.0304                        0.59
## 231            2.1654      1.0184                        0.01
## 233           26.5976      1.0280                        0.25
## 234           41.7665      1.0204                        0.20
## 
## $Asia
##     Rank CCA3    Country.Territory             Capital Continent
## 1     36  AFG          Afghanistan               Kabul      Asia
## 10   140  ARM              Armenia             Yerevan      Asia
## 14    91  AZE           Azerbaijan                Baku      Asia
## 16   154  BHR              Bahrain              Manama      Asia
## 17     8  BGD           Bangladesh               Dhaka      Asia
## 24   165  BTN               Bhutan             Thimphu      Asia
## 30   175  BRN               Brunei Bandar Seri Begawan      Asia
## 34    73  KHM             Cambodia          Phnom Penh      Asia
## 42     1  CHN                China             Beijing      Asia
## 74   131  GEO              Georgia             Tbilisi      Asia
## 90   104  HKG            Hong Kong           Hong Kong      Asia
## 93     2  IND                India           New Delhi      Asia
## 94     4  IDN            Indonesia             Jakarta      Asia
## 95    17  IRN                 Iran              Tehran      Asia
## 96    35  IRQ                 Iraq             Baghdad      Asia
## 99    98  ISR               Israel           Jerusalem      Asia
## 103   11  JPN                Japan               Tokyo      Asia
## 105   83  JOR               Jordan               Amman      Asia
## 106   66  KAZ           Kazakhstan           Nursultan      Asia
## 109  129  KWT               Kuwait         Kuwait City      Asia
## 110  110  KGZ           Kyrgyzstan             Bishkek      Asia
## 111  103  LAO                 Laos           Vientiane      Asia
## 113  119  LBN              Lebanon              Beirut      Asia
## 120  167  MAC                Macau   Concelho de Macau      Asia
## 123   45  MYS             Malaysia        Kuala Lumpur      Asia
## 124  174  MDV             Maldives                Malé      Asia
## 136  134  MNG             Mongolia         Ulaanbaatar      Asia
## 141   26  MMR              Myanmar         Nay Pyi Taw      Asia
## 144   49  NPL                Nepal           Kathmandu      Asia
## 152   56  PRK          North Korea           Pyongyang      Asia
## 156  127  OMN                 Oman              Muscat      Asia
## 157    5  PAK             Pakistan           Islamabad      Asia
## 159  122  PSE            Palestine            Ramallah      Asia
## 164   13  PHL          Philippines              Manila      Asia
## 168  143  QAT                Qatar                Doha      Asia
## 183   41  SAU         Saudi Arabia              Riyadh      Asia
## 188  113  SGP            Singapore           Singapore      Asia
## 195   29  KOR          South Korea               Seoul      Asia
## 198   61  LKA            Sri Lanka             Colombo      Asia
## 203   60  SYR                Syria            Damascus      Asia
## 204   57  TWN               Taiwan              Taipei      Asia
## 205   95  TJK           Tajikistan            Dushanbe      Asia
## 207   20  THA             Thailand             Bangkok      Asia
## 208  155  TLS          Timor-Leste                Dili      Asia
## 214   18  TUR               Turkey              Ankara      Asia
## 215  111  TKM         Turkmenistan            Ashgabat      Asia
## 220   97  ARE United Arab Emirates           Abu Dhabi      Asia
## 225   43  UZB           Uzbekistan            Tashkent      Asia
## 229   16  VNM              Vietnam               Hanoi      Asia
## 232   46  YEM                Yemen               Sanaa      Asia
##     X2022.Population X2020.Population X2015.Population X2010.Population
## 1           41128771         38972230         33753499         28189672
## 10           2780469          2805608          2878595          2946293
## 14          10358074         10284951          9863480          9237202
## 16           1472233          1477469          1362142          1213645
## 17         171186372        167420951        157830000        148391139
## 24            782455           772506           743274           705516
## 30            449002           441725           421437           396053
## 34          16767842         16396860         15417523         14363532
## 42        1425887337       1424929781       1393715448       1348191368
## 74           3744385          3765912          3771132          3836831
## 90           7488865          7500958          7399838          7132438
## 93        1417173173       1396387127       1322866505       1240613620
## 94         275501339        271857970        259091970        244016173
## 95          88550570         87290193         81790841         75373855
## 96          44496122         42556984         37757813         31264875
## 99           9038309          8757489          8007778          7328445
## 103        123951692        125244761        127250933        128105431
## 105         11285869         10928721          9494246          6931258
## 106         19397998         18979243         17835909         16627837
## 109          4268873          4360444          3908743          2943356
## 110          6630623          6424874          5914980          5483774
## 111          7529475          7319399          6787419          6323418
## 113          5489739          5662923          6398940          4995800
## 120           695168           676283           615239           557297
## 123         33938221         33199993         31068833         28717731
## 124           523787           514438           435582           361575
## 136          3398366          3294335          2964749          2702520
## 141         54179306         53423198         51483949         49390988
## 144         30547580         29348627         27610325         27161567
## 152         26069416         25867467         25258015         24686435
## 156          4576298          4543399          4191776          2881914
## 157        235824862        227196741        210969298        194454498
## 159          5250072          5019401          4484614          3992278
## 164        115559009        112190977        103031365         94636700
## 168          2695122          2760385          2414573          1713504
## 183         36408820         35997107         32749848         29411929
## 188          5975689          5909869          5650018          5163590
## 195         51815810         51844690         50994401         48813042
## 198         21832143         21715079         21336697         20668557
## 203         22125249         20772595         19205178         22337563
## 204         23893394         23821464         23512136         23083083
## 205          9952787          9543207          8524063          7621779
## 207         71697030         71475664         70294397         68270489
## 208          1341296          1299995          1205813          1088486
## 214         85341241         84135428         79646178         73195345
## 215          6430770          6250438          5766431          5267970
## 220          9441129          9287289          8916899          8481771
## 225         34627652         33526656         30949417         28614227
## 229         98186856         96648685         92191398         87411012
## 232         33696614         32284046         28516545         24743946
##     X2000.Population X1990.Population X1980.Population X1970.Population
## 1           19542982         10694796         12486631         10752971
## 10           3168523          3556539          3135123          2534377
## 14           8190337          7427836          6383060          5425317
## 16            711442           517418           362595           222555
## 17         129193327        107147651         83929765         67541860
## 24            587207           558442           415257           298894
## 30            333926           261928           187921           133343
## 34          12118841          8910808          6198959          6708525
## 42        1264099069       1153704252        982372466        822534450
## 74           4265172          5391636          5145843          4800426
## 90           6731195          5838574          4978544          3955072
## 93        1059633675        870452165        696828385        557501301
## 94         214072421        182159874        148177096        115228394
## 95          65544383         55793629         38520664         28449705
## 96          24628858         17658381         13653369          9811347
## 99           6116958          4803254          3744608          2907307
## 103        126803861        123686321        117624196        105416839
## 105          5056174          3480587          2216903          1557374
## 106         15236253         16866563         14172710         12265305
## 109          1934901          1674938          1493870           802786
## 110          4935182          4394734          3691209          3016384
## 111          5430853          4314443          3297519          2675283
## 113          4320642          3593700          2963702          2381791
## 120           431896           350227           245332           247284
## 123         22945150         17517054         13215707         10306508
## 124           282507           224957           164887           123243
## 136          2450979          2161433          1697780          1293880
## 141         45538332         40099553         33465781         27284112
## 144         24559500         19616530         15600442         12501285
## 152         23367059         20799523         17973650         14996879
## 156          2344253          1804524          1017462           670693
## 157        154369924        115414069         80624057         59290872
## 159          3139954          2124609          1453620          1118241
## 164         77958223         61558898         48419546         37435586
## 168           645937           441675           277450           118007
## 183         21547390         16004763         10171710          6106191
## 188          4053602          3022209          2400729          2061831
## 195         46788591         44120039         38170501         32601143
## 198         18776371         17204094         14943645         12388769
## 203         16307654         12408996          8898954          6319199
## 204         22194731         20586174         18100281         14957870
## 205          6272998          5417860          4045965          2993019
## 207         63066603         55228410         45737753         35791728
## 208           878360           758106           642224           554021
## 214         64113547         54324142         44089069         35540990
## 215          4569132          3720278          2862903          2201432
## 220          3275333          1900151          1014048           298084
## 225         24925554         20579100         15947129         12011361
## 229         79001142         66912613         52968270         41928849
## 232         18628700         13375121          9204938          6843607
##     Area..km.. Density..per.km.. Growth.Rate World.Population.Percentage
## 1       652230           63.0587      1.0257                        0.52
## 10       29743           93.4831      0.9962                        0.03
## 14       86600          119.6082      1.0044                        0.13
## 16         765         1924.4876      1.0061                        0.02
## 17      147570         1160.0350      1.0108                        2.15
## 24       38394           20.3796      1.0064                        0.01
## 30        5765           77.8841      1.0081                        0.01
## 34      181035           92.6221      1.0108                        0.21
## 42     9706961          146.8933      1.0000                       17.88
## 74       69700           53.7214      0.9964                        0.05
## 90        1104         6783.3922      0.9992                        0.09
## 93     3287590          431.0675      1.0068                       17.77
## 94     1904569          144.6529      1.0064                        3.45
## 95     1648195           53.7258      1.0071                        1.11
## 96      438317          101.5158      1.0221                        0.56
## 99       20770          435.1617      1.0155                        0.11
## 103     377930          327.9753      0.9947                        1.55
## 105      89342          126.3221      1.0123                        0.14
## 106    2724900            7.1188      1.0105                        0.24
## 109      17818          239.5821      1.0044                        0.05
## 110     199951           33.1612      1.0158                        0.08
## 111     236800           31.7968      1.0141                        0.09
## 113      10452          525.2334      0.9816                        0.07
## 120         30        23172.2667      1.0125                        0.01
## 123     330803          102.5934      1.0109                        0.43
## 124        300         1745.9567      1.0045                        0.01
## 136    1564110            2.1727      1.0151                        0.04
## 141     676578           80.0784      1.0071                        0.68
## 144     147181          207.5511      1.0171                        0.38
## 152     120538          216.2755      1.0038                        0.33
## 156     309500           14.7861      1.0123                        0.06
## 157     881912          267.4018      1.0191                        2.96
## 159       6220          844.0630      1.0227                        0.07
## 164     342353          337.5434      1.0147                        1.45
## 168      11586          232.6189      1.0026                        0.03
## 183    2149690           16.9368      1.0128                        0.46
## 188        710         8416.4634      1.0058                        0.07
## 195     100210          517.0722      0.9997                        0.65
## 198      65610          332.7563      1.0027                        0.27
## 203     185180          119.4797      1.0376                        0.28
## 204      36193          660.1662      1.0014                        0.30
## 205     143100           69.5513      1.0208                        0.12
## 207     513120          139.7276      1.0013                        0.90
## 208      14874           90.1772      1.0154                        0.02
## 214     783562          108.9145      1.0067                        1.07
## 215     488100           13.1751      1.0140                        0.08
## 220      83600          112.9322      1.0081                        0.12
## 225     447400           77.3975      1.0160                        0.43
## 229     331212          296.4472      1.0074                        1.23
## 232     527968           63.8232      1.0217                        0.42
## 
## $Europe
##     Rank CCA3      Country.Territory          Capital Continent
## 2    138  ALB                Albania           Tirana    Europe
## 5    203  AND                Andorra Andorra la Vella    Europe
## 13    99  AUT                Austria           Vienna    Europe
## 19    96  BLR                Belarus            Minsk    Europe
## 20    81  BEL                Belgium         Brussels    Europe
## 26   137  BIH Bosnia and Herzegovina         Sarajevo    Europe
## 31   108  BGR               Bulgaria            Sofia    Europe
## 47   130  HRV                Croatia           Zagreb    Europe
## 50   158  CYP                 Cyprus          Nicosia    Europe
## 51    88  CZE         Czech Republic           Prague    Europe
## 52   115  DNK                Denmark       Copenhagen    Europe
## 62   156  EST                Estonia          Tallinn    Europe
## 66   209  FRO          Faroe Islands         Tórshavn    Europe
## 68   118  FIN                Finland         Helsinki    Europe
## 69    23  FRA                 France            Paris    Europe
## 75    19  DEU                Germany           Berlin    Europe
## 77   219  GIB              Gibraltar        Gibraltar    Europe
## 78    90  GRC                 Greece           Athens    Europe
## 84   207  GGY               Guernsey Saint Peter Port    Europe
## 91    94  HUN                Hungary         Budapest    Europe
## 92   179  ISL                Iceland        Reykjavík    Europe
## 97   125  IRL                Ireland           Dublin    Europe
## 98   202  IMN            Isle of Man          Douglas    Europe
## 100   25  ITA                  Italy             Rome    Europe
## 104  195  JEY                 Jersey     Saint Helier    Europe
## 112  151  LVA                 Latvia             Riga    Europe
## 117  216  LIE          Liechtenstein            Vaduz    Europe
## 118  141  LTU              Lithuania          Vilnius    Europe
## 119  168  LUX             Luxembourg       Luxembourg    Europe
## 126  173  MLT                  Malta         Valletta    Europe
## 134  135  MDA                Moldova         Chisinau    Europe
## 135  217  MCO                 Monaco           Monaco    Europe
## 137  169  MNE             Montenegro        Podgorica    Europe
## 145   71  NLD            Netherlands        Amsterdam    Europe
## 153  150  MKD        North Macedonia           Skopje    Europe
## 155  120  NOR                 Norway             Oslo    Europe
## 165   37  POL                 Poland           Warsaw    Europe
## 166   92  PRT               Portugal           Lisbon    Europe
## 171   64  ROU                Romania        Bucharest    Europe
## 172    9  RUS                 Russia           Moscow    Europe
## 181  218  SMR             San Marino       San Marino    Europe
## 185  105  SRB                 Serbia         Belgrade    Europe
## 190  116  SVK               Slovakia       Bratislava    Europe
## 191  148  SVN               Slovenia        Ljubljana    Europe
## 197   30  ESP                  Spain           Madrid    Europe
## 201   87  SWE                 Sweden        Stockholm    Europe
## 202  101  CHE            Switzerland             Bern    Europe
## 219   38  UKR                Ukraine             Kiev    Europe
## 221   21  GBR         United Kingdom           London    Europe
## 227  234  VAT           Vatican City     Vatican City    Europe
##     X2022.Population X2020.Population X2015.Population X2010.Population
## 2            2842321          2866849          2882481          2913399
## 5              79824            77700            71746            71519
## 13           8939617          8907777          8642421          8362829
## 19           9534954          9633740          9700609          9731427
## 20          11655930         11561717         11248303         10877947
## 26           3233526          3318407          3524324          3811088
## 31           6781953          6979175          7309253          7592273
## 47           4030358          4096868          4254815          4368682
## 50           1251488          1237537          1187280          1129686
## 51          10493986         10530953         10523798         10464749
## 52           5882261          5825641          5677796          5550849
## 62           1326062          1329444          1314657          1331535
## 66             53090            52415            48816            48410
## 68           5540745          5529468          5479461          5363271
## 69          64626628         64480053         63809769         62444567
## 75          83369843         83328988         82073226         81325090
## 77             32649            32709            32520            31262
## 78          10384971         10512232         10806641         11033783
## 84             63301            62794            61629            60782
## 91           9967308          9750573          9844246          9986825
## 92            372899           366669           331060           318333
## 97           5023109          4946119          4665760          4524585
## 98             84519            84046            83593            83828
## 100         59037474         59500579         60232906         59822450
## 104           110778           108319           100561            96151
## 112          1850651          1897052          1991955          2101530
## 117            39327            38756            37355            35926
## 118          2750055          2820267          2963765          3139019
## 119           647599           630399           569408           507070
## 126           533286           515357           456579           418755
## 134          3272996          3084847          3277388          3678186
## 135            36469            36922            36760            33178
## 137           627082           629048           633966           631044
## 145         17564014         17434557         17041107         16617116
## 153          2093599          2111072          2107962          2093828
## 155          5434319          5379839          5190356          4889741
## 165         39857145         38428366         38553146         38597353
## 166         10270865         10298192         10365435         10588401
## 171         19659267         19442038         19906079         20335211
## 172        144713314        145617329        144668389        143242599
## 181            33660            34007            33570            31608
## 185          7221365          7358005          7519496          7653748
## 190          5643453          5456681          5424444          5396424
## 191          2119844          2117641          2080862          2057286
## 197         47558630         47363807         46431342         46572772
## 201         10549347         10368969          9849349          9381729
## 202          8740472          8638613          8281732          7822435
## 219         39701739         43909666         44982564         45683020
## 221         67508936         67059474         65224364         62760039
## 227              510              520              564              596
##     X2000.Population X1990.Population X1980.Population X1970.Population
## 2            3182021          3295066          2941651          2324731
## 5              66097            53569            35611            19860
## 13           8010428          7678729          7547561          7465301
## 19          10256483         10428525          9817257          9170786
## 20          10264343          9959560          9828986          9629376
## 26           4179350          4494310          4199820          3815561
## 31           8097691          8767778          8980606          8582950
## 47           4548434          4873707          4680144          4492638
## 50            948237           788500           679327           640804
## 51          10234710         10301192         10270060          9795744
## 52           5340655          5144623          5125392          4922963
## 62           1396877          1570674          1476983          1361999
## 66             45660            47479            43054            38416
## 68           5176209          4986545          4779418          4606621
## 69          58665453         56412897         53713830         50523586
## 75          81551677         79370196         77786703         78294583
## 77             27741            27317            28734            26685
## 78          11038109         10302255          9307148          8544873
## 84             59114            57727            52860            52656
## 91          10202055         10375989         10698679         10315366
## 92            281462           255019           228263           204468
## 97           3768950          3485374          3391387          2937637
## 98             75562            68865            64022            55298
## 100         56966397         56756561         56329482         53324036
## 104            86192            82874            75124            68347
## 112          2392530          2689391          2572037          2397414
## 117            33026            28765            25003            21089
## 118          3599637          3785847          3521206          3210147
## 119           435628           381267           363741           339342
## 126           399212           365392           333587           315414
## 134          4251573          4480199          4103240          3711140
## 135            32465            30329            27076            24270
## 137           633324           621442           589324           530268
## 145         15899135         14944548         14130387         13037686
## 153          2037936          2044174          1907023          1656783
## 155          4491202          4241636          4085776          3875546
## 165         38504431         38064255         35521429         32482943
## 166         10300626         10007346          9785252          8683631
## 171         21919876         22836234         22125224         19922618
## 172        146844839        148005704        138257420        130093010
## 181            26823            23132            21346            18169
## 185          7935022          7987529          7777010          7193533
## 190          5376690          5261305          4973883          4522867
## 191          1984339          1986024          1901570          1741286
## 197         40741651         38889889         37491666         33792617
## 201          8871043          8548406          8311763          8027702
## 202          7182059          6711693          6319113          6181227
## 219         48879755         51589817         49973920         47279086
## 221         58850043         57210442         56326328         55650166
## 227              651              700              733              752
##     Area..km.. Density..per.km.. Growth.Rate World.Population.Percentage
## 2        28748           98.8702      0.9957                        0.04
## 5          468          170.5641      1.0100                        0.00
## 13       83871          106.5877      1.0020                        0.11
## 19      207600           45.9295      0.9955                        0.12
## 20       30528          381.8111      1.0038                        0.15
## 26       51209           63.1437      0.9886                        0.04
## 31      110879           61.1654      0.9849                        0.09
## 47       56594           71.2153      0.9927                        0.05
## 50        9251          135.2814      1.0059                        0.02
## 51       78865          133.0627      0.9984                        0.13
## 52       43094          136.4984      1.0048                        0.07
## 62       45227           29.3201      0.9980                        0.02
## 66        1393           38.1120      1.0038                        0.00
## 68      338424           16.3722      1.0009                        0.07
## 69      551695          117.1419      1.0015                        0.81
## 75      357114          233.4544      0.9995                        1.05
## 77           6         5441.5000      0.9994                        0.00
## 78      131990           78.6800      0.9942                        0.13
## 84          78          811.5513      1.0037                        0.00
## 91       93028          107.1431      1.0265                        0.12
## 92      103000            3.6204      1.0069                        0.00
## 97       70273           71.4799      1.0073                        0.06
## 98         572          147.7605      1.0030                        0.00
## 100     301336          195.9191      0.9966                        0.74
## 104        116          954.9828      1.0106                        0.00
## 112      64559           28.6660      0.9876                        0.02
## 117        160          245.7937      1.0074                        0.00
## 118      65300           42.1142      0.9869                        0.03
## 119       2586          250.4250      1.0129                        0.01
## 126        316         1687.6139      1.0124                        0.01
## 134      33846           96.7026      1.0691                        0.04
## 135          2        18234.5000      0.9941                        0.00
## 137      13812           45.4012      0.9988                        0.01
## 145      41850          419.6897      1.0036                        0.22
## 153      25713           81.4218      0.9954                        0.03
## 155     323802           16.7828      1.0058                        0.07
## 165     312679          127.4698      1.0404                        0.50
## 166      92090          111.5307      0.9981                        0.13
## 171     238391           82.4665      1.0171                        0.25
## 172   17098242            8.4636      0.9973                        1.81
## 181         61          551.8033      0.9975                        0.00
## 185      88361           81.7257      0.9897                        0.09
## 190      49037          115.0856      1.0359                        0.07
## 191      20273          104.5649      1.0002                        0.03
## 197     505992           93.9909      1.0015                        0.60
## 201     450295           23.4276      1.0079                        0.13
## 202      41284          211.7157      1.0056                        0.11
## 219     603500           65.7858      0.9120                        0.50
## 221     242900          277.9289      1.0034                        0.85
## 227          1          510.0000      0.9980                        0.00
## 
## $`North America`
##     Rank CCA3                Country.Territory          Capital     Continent
## 7    224  AIA                         Anguilla       The Valley North America
## 8    201  ATG              Antigua and Barbuda     Saint John’s North America
## 11   198  ABW                            Aruba       Oranjestad North America
## 15   176  BHS                          Bahamas           Nassau North America
## 18   186  BRB                         Barbados       Bridgetown North America
## 21   177  BLZ                           Belize         Belmopan North America
## 23   206  BMU                          Bermuda         Hamilton North America
## 29   221  VGB           British Virgin Islands        Road Town North America
## 36    39  CAN                           Canada           Ottawa North America
## 38   205  CYM                   Cayman Islands      George Town North America
## 46   124  CRI                       Costa Rica         San José North America
## 48    85  CUB                             Cuba           Havana North America
## 49   189  CUW                          Curacao       Willemstad North America
## 54   204  DMA                         Dominica           Roseau North America
## 55    84  DOM               Dominican Republic    Santo Domingo North America
## 59   112  SLV                      El Salvador     San Salvador North America
## 79   208  GRL                        Greenland             Nuuk North America
## 80   193  GRD                          Grenada   Saint George's North America
## 81   178  GLP                       Guadeloupe      Basse-Terre North America
## 83    68  GTM                        Guatemala   Guatemala City North America
## 88    82  HTI                            Haiti   Port-au-Prince North America
## 89    89  HND                         Honduras      Tegucigalpa North America
## 102  139  JAM                          Jamaica         Kingston North America
## 128  180  MTQ                       Martinique   Fort-de-France North America
## 132   10  MEX                           Mexico      Mexico City North America
## 138  230  MSR                       Montserrat           Brades North America
## 148  106  NIC                        Nicaragua          Managua North America
## 160  128  PAN                           Panama      Panama City North America
## 167  136  PRI                      Puerto Rico         San Juan North America
## 174  228  BLM                 Saint Barthelemy         Gustavia North America
## 175  211  KNA            Saint Kitts and Nevis       Basseterre North America
## 176  190  LCA                      Saint Lucia         Castries North America
## 177  220  MAF                     Saint Martin          Marigot North America
## 178  229  SPM        Saint Pierre and Miquelon     Saint-Pierre North America
## 179  199  VCT Saint Vincent and the Grenadines        Kingstown North America
## 189  214  SXM                     Sint Maarten      Philipsburg North America
## 212  153  TTO              Trinidad and Tobago    Port-of-Spain North America
## 216  212  TCA         Turks and Caicos Islands    Cockburn Town North America
## 222    3  USA                    United States Washington, D.C. North America
## 223  200  VIR     United States Virgin Islands Charlotte Amalie North America
##     X2022.Population X2020.Population X2015.Population X2010.Population
## 7              15857            15585            14525            13172
## 8              93763            92664            89941            85695
## 11            106445           106585           104257           100341
## 15            409984           406471           392697           373272
## 18            281635           280693           278083           274711
## 21            405272           394921           359871           322106
## 23             64184            64031            63144            63447
## 29             31305            30910            29366            27556
## 36          38454327         37888705         35732126         33963412
## 38             68706            67311            60911            54074
## 46           5180829          5123105          4895242          4622252
## 48          11212191         11300698         11339894         11290417
## 49            191163           189288           169572           159380
## 54             72737            71995            70007            68755
## 55          11228821         10999664         10405832          9775755
## 59           6336392          6292731          6231066          6114034
## 79             56466            56026            55895            56351
## 80            125438           123663           118980           114039
## 81            395752           395642           399089           403072
## 83          17843908         17362718         16001107         14543121
## 88          11584996         11306801         10563757          9842880
## 89          10432860         10121763          9294505          8450933
## 102          2827377          2820436          2794445          2733896
## 128           367507           370391           383515           392181
## 132        127504125        125998302        120149897        112532401
## 138             4390             4500             5059             4938
## 148          6948392          6755895          6298598          5855734
## 160          4408581          4294396          3957099          3623617
## 167          3252407          3271564          3497335          3717922
## 174            10967            10681             9643             8988
## 175            47657            47642            47790            47403
## 176           179857           179237           175623           170935
## 177            31791            32552            35020            36458
## 178             5862             5906             5978             6052
## 179           103948           104632           106482           109308
## 189            44175            43621            40205            33034
## 212          1531044          1518147          1460177          1410296
## 216            45703            44276            36538            29726
## 222        338289857        335942003        324607776        311182845
## 223            99465           100442           102803           106142
##     X2000.Population X1990.Population X1980.Population X1970.Population
## 7              11047             8316             6560             6283
## 8              75055            63328            64888            64516
## 11             89101            65712            62267            59106
## 15            325014           270679           223752           179129
## 18            264657           258868           253575           241397
## 21            240406           182589           145133           120905
## 23             61371            57470            53565            52019
## 29             20104            15617            11109             9581
## 36          30683313         27657204         24511510         21434577
## 38             39658            26027            17100            10533
## 46           3979193          3158253          2414303          1855697
## 48          11105791         10626680          9809107          8869636
## 49            141424           155446           156851           150385
## 54             68346            69481            72978            68895
## 55           8540791          7129004          5755800          4475871
## 59           5958482          5367179          4508992          3619090
## 79             56184            55599            50106            45434
## 80            107432            99047            94838            98794
## 81            424067           391951           334234           318310
## 83          11735894          9084780          6987767          5453208
## 88           8360225          6925331          5646676          4680812
## 89           6656725          5053234          3777990          2782753
## 102          2612205          2392030          2135546          1859091
## 128           432543           374271           333786           326428
## 132         97873442         81720428         67705186         50289306
## 138             5138            10805            11452            11402
## 148          5123222          4227820          3303309          2444767
## 160          3001731          2449968          1956987          1516188
## 167          3827108          3543776          3214568          2737619
## 174             7082             5168             2983             2417
## 175            45461            40636            43097            44968
## 176           159500           142301           121633           103090
## 177            29610            28127             7776             5802
## 178             6274             6324             6106             5537
## 179           113813           112487           107480            98459
## 189            30489            27845            12243             6260
## 212          1332203          1266518          1127852           988890
## 216            18744            11709             7598             5665
## 222        282398554        248083732        223140018        200328340
## 223           108185           100685            96640            63446
##     Area..km.. Density..per.km.. Growth.Rate World.Population.Percentage
## 7           91          174.2527      1.0066                        0.00
## 8          442          212.1335      1.0058                        0.00
## 11         180          591.3611      0.9991                        0.00
## 15       13943           29.4043      1.0051                        0.01
## 18         430          654.9651      1.0015                        0.00
## 21       22966           17.6466      1.0131                        0.01
## 23          54         1188.5926      1.0000                        0.00
## 29         151          207.3179      1.0059                        0.00
## 36     9984670            3.8513      1.0078                        0.48
## 38         264          260.2500      1.0084                        0.00
## 46       51100          101.3861      1.0052                        0.06
## 48      109884          102.0366      0.9961                        0.14
## 49         444          430.5473      1.0043                        0.00
## 54         751           96.8535      1.0045                        0.00
## 55       48671          230.7087      1.0100                        0.14
## 59       21041          301.1450      1.0035                        0.08
## 79     2166086            0.0261      1.0040                        0.00
## 80         344          364.6453      1.0066                        0.00
## 81        1628          243.0909      0.9992                        0.00
## 83      108889          163.8725      1.0134                        0.22
## 88       27750          417.4773      1.0120                        0.15
## 89      112492           92.7431      1.0150                        0.13
## 102      10991          257.2447      0.9999                        0.04
## 128       1128          325.8041      0.9965                        0.00
## 132    1964375           64.9082      1.0063                        1.60
## 138        102           43.0392      0.9939                        0.00
## 148     130373           53.2962      1.0143                        0.09
## 160      75417           58.4561      1.0132                        0.06
## 167       8870          366.6750      0.9989                        0.04
## 174         21          522.2381      1.0098                        0.00
## 175        261          182.5939      1.0011                        0.00
## 176        616          291.9756      1.0011                        0.00
## 177         53          599.8302      0.9951                        0.00
## 178        242           24.2231      0.9964                        0.00
## 179        389          267.2185      0.9963                        0.00
## 189         34         1299.2647      1.0030                        0.00
## 212       5130          298.4491      1.0035                        0.02
## 216        948           48.2099      1.0131                        0.00
## 222    9372610           36.0935      1.0038                        4.24
## 223        347          286.6427      0.9937                        0.00
## 
## $Oceania
##     Rank CCA3        Country.Territory      Capital Continent X2022.Population
## 4    213  ASM           American Samoa    Pago Pago   Oceania            44273
## 12    55  AUS                Australia     Canberra   Oceania         26177413
## 45   223  COK             Cook Islands       Avarua   Oceania            17011
## 67   162  FJI                     Fiji         Suva   Oceania           929766
## 71   183  PYF         French Polynesia      Papeete   Oceania           306279
## 82   191  GUM                     Guam      Hagåtña   Oceania           171774
## 108  192  KIR                 Kiribati       Tarawa   Oceania           131232
## 127  215  MHL         Marshall Islands       Majuro   Oceania            41569
## 133  194  FSM               Micronesia      Palikir   Oceania           114164
## 143  225  NRU                    Nauru        Yaren   Oceania            12668
## 146  185  NCL            New Caledonia       Nouméa   Oceania           289950
## 147  123  NZL              New Zealand   Wellington   Oceania          5185288
## 151  232  NIU                     Niue        Alofi   Oceania             1934
## 154  210  NFK Northern Mariana Islands       Saipan   Oceania            49551
## 158  222  PLW                    Palau    Ngerulmud   Oceania            18055
## 161   93  PNG         Papua New Guinea Port Moresby   Oceania         10142619
## 180  188  WSM                    Samoa         Apia   Oceania           222382
## 192  166  SLB          Solomon Islands      Honiara   Oceania           724273
## 210  233  TKL                  Tokelau     Nukunonu   Oceania             1871
## 211  197  TON                    Tonga   Nuku‘alofa   Oceania           106858
## 217  227  TUV                   Tuvalu     Funafuti   Oceania            11312
## 226  181  VUT                  Vanuatu    Port-Vila   Oceania           326740
## 230  226  WLF        Wallis and Futuna     Mata-Utu   Oceania            11572
##     X2020.Population X2015.Population X2010.Population X2000.Population
## 4              46189            51368            54849            58230
## 12          25670051         23820236         22019168         19017963
## 45             17029            17695            17212            15897
## 67            920422           917200           905169           832509
## 71            301920           291787           283788           250927
## 82            169231           167978           164905           160188
## 108           126463           116707           107995            88826
## 127            43413            49410            53416            54224
## 133           112106           109462           107588           111709
## 143            12315            11185            10241            10377
## 146           286403           283032           261426           221537
## 147          5061133          4590590          4346338          3855266
## 151             1942             1847             1812             2074
## 154            49587            51514            54087            80338
## 158            17972            17794            18540            19726
## 161          9749640          8682174          7583269          5508297
## 180           214929           203571           194672           184008
## 192           691191           612660           540394           429978
## 210             1827             1454             1367             1666
## 211           105254           106122           107383           102603
## 217            11069            10877            10550             9638
## 226           311685           276438           245453           192074
## 230            11655            12182            13142            14723
##     X1990.Population X1980.Population X1970.Population Area..km..
## 4              47818            32886            27075        199
## 12          17048003         14706322         12595034    7692024
## 45             17123            17651            20470        236
## 67            780430           644582           527634      18272
## 71            211089           163591           117891       4167
## 82            138263           110286            88300        549
## 108            75124            60813            57437        811
## 127            46047            31988            23969        181
## 133            98603            76299            58989        702
## 143             9598             7635             6663         21
## 146           177264           148599           110982      18575
## 147          3397389          3147168          2824061     270467
## 151             2533             3637             5185        260
## 154            48002            17613            10143        464
## 158            15293            12252            11366        459
## 161          3864972          3104788          2489059     462840
## 180           168186           164905           142771       2842
## 192           324171           233668           172833      28896
## 210             1669             1647             1714         12
## 211            98727            96708            86484        747
## 217             9182             7731             5814         26
## 226           150882           118156            87019      12189
## 230            13454            11315             9377        142
##     Density..per.km.. Growth.Rate World.Population.Percentage
## 4            222.4774      0.9831                        0.00
## 12             3.4032      1.0099                        0.33
## 45            72.0805      1.0005                        0.00
## 67            50.8847      1.0056                        0.01
## 71            73.5011      1.0074                        0.00
## 82           312.8852      1.0073                        0.00
## 108          161.8150      1.0183                        0.00
## 127          229.6630      0.9886                        0.00
## 133          162.6268      1.0091                        0.00
## 143          603.2381      1.0125                        0.00
## 146           15.6097      1.0075                        0.00
## 147           19.1716      1.0108                        0.07
## 151            7.4385      0.9985                        0.00
## 154          106.7909      1.0014                        0.00
## 158           39.3355      1.0017                        0.00
## 161           21.9139      1.0194                        0.13
## 180           78.2484      1.0165                        0.00
## 192           25.0648      1.0232                        0.01
## 210          155.9167      1.0119                        0.00
## 211          143.0495      1.0079                        0.00
## 217          435.0769      1.0096                        0.00
## 226           26.8061      1.0238                        0.00
## 230           81.4930      0.9953                        0.00
## 
## $`South America`
##     Rank CCA3 Country.Territory      Capital     Continent X2022.Population
## 9     33  ARG         Argentina Buenos Aires South America         45510318
## 25    80  BOL           Bolivia        Sucre South America         12224110
## 28     7  BRA            Brazil     Brasilia South America        215313498
## 41    65  CHL             Chile     Santiago South America         19603733
## 43    28  COL          Colombia       Bogota South America         51874024
## 57    67  ECU           Ecuador        Quito South America         18001000
## 65   231  FLK  Falkland Islands      Stanley South America             3780
## 70   184  GUF     French Guiana      Cayenne South America           304557
## 87   164  GUY            Guyana   Georgetown South America           808726
## 162  109  PRY          Paraguay     Asunción South America          6780744
## 163   44  PER              Peru         Lima South America         34049588
## 200  170  SUR          Suriname   Paramaribo South America           618040
## 224  133  URY           Uruguay   Montevideo South America          3422794
## 228   51  VEN         Venezuela      Caracas South America         28301696
##     X2020.Population X2015.Population X2010.Population X2000.Population
## 9           45036032         43257065         41100123         37070774
## 25          11936162         11090085         10223270          8592656
## 28         213196304        205188205        196353492        175873720
## 41          19300315         17870124         17004162         15351799
## 43          50930662         47119728         44816108         39215135
## 57          17588595         16195902         14989585         12626507
## 65              3747             3408             3187             3080
## 70            290969           257026           228453           164351
## 87            797202           755031           747932           759051
## 162          6618695          6177950          5768613          5123819
## 163         33304756         30711863         29229572         26654439
## 200           607065           575475           546080           478998
## 224          3429086          3402818          3352651          3292224
## 228         28490453         30529716         28715022         24427729
##     X1990.Population X1980.Population X1970.Population Area..km..
## 9           32637657         28024803         23842803    2780400
## 25           7096194          5736088          4585693    1098581
## 28         150706446        122288383         96369875    8515767
## 41          13342868         11469828          9820481     756102
## 43          32601393         26176195         20905254    1141748
## 57          10449837          8135845          6172215     276841
## 65              2332             2240             2274      12173
## 70            113931            66825            46484      83534
## 87            747116           778176           705261     214969
## 162          4059195          3078912          2408787     406752
## 163         22109099         17492406         13562371    1285216
## 200           412756           375112           379918     163820
## 224          3117012          2953750          2790265     181034
## 228         19750579         15210443         11355475     916445
##     Density..per.km.. Growth.Rate World.Population.Percentage
## 9             16.3683      1.0052                        0.57
## 25            11.1272      1.0120                        0.15
## 28            25.2841      1.0046                        2.70
## 41            25.9274      1.0057                        0.25
## 43            45.4339      1.0069                        0.65
## 57            65.0229      1.0114                        0.23
## 65             0.3105      1.0043                        0.00
## 70             3.6459      1.0239                        0.00
## 87             3.7621      1.0052                        0.01
## 162           16.6705      1.0115                        0.09
## 163           26.4933      1.0099                        0.43
## 200            3.7727      1.0082                        0.01
## 224           18.9069      0.9990                        0.04
## 228           30.8820      1.0036                        0.35
avg_pop_by_continent <- lapply(continents_list, function(sub_df) {
  mean(sub_df$`2022 Population`, na.rm = TRUE)
})
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
## Warning in mean.default(sub_df$`2022 Population`, na.rm = TRUE): argument is
## not numeric or logical: returning NA
avg_pop_by_continent
## $Africa
## [1] NA
## 
## $Asia
## [1] NA
## 
## $Europe
## [1] NA
## 
## $`North America`
## [1] NA
## 
## $Oceania
## [1] NA
## 
## $`South America`
## [1] NA
##check quantitative variables(numeric)

Quantitative<- sapply(wp[sapply(wp,is.numeric)],sum)
Quantitative
##                        Rank            X2022.Population 
##                2.749500e+04                7.973413e+09 
##            X2020.Population            X2015.Population 
##                7.839251e+09                7.424810e+09 
##            X2010.Population            X2000.Population 
##                6.983785e+09                6.147056e+09 
##            X1990.Population            X1980.Population 
##                5.314192e+09                4.442400e+09 
##            X1970.Population                  Area..km.. 
##                3.694137e+09                1.360592e+08 
##           Density..per.km..                 Growth.Rate 
##                1.057977e+05                2.362411e+02 
## World.Population.Percentage 
##                9.993000e+01

#detect outliers

boxplot(Quantitative,las=2,main="quantitative variables")

##4.3Generating new Variable

#HW3:use of “group_by” :help to perform calculations within each group instead of on the whole dataset. ##use of %>%(pipe operator):pass the result of one operation directly into the next function, making code easier to read and write. ##all these are from from the magrittr package, commonly used in library(dplyr) to manipulate and transform dataset. #there are several function of this library to manipulate data such as: #mutate(),select(),arrange(),filter(),..

library(dplyr)
## 
## 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
t<-8
wp<- wp%>%
mutate(X2030.population=X2022.Population*exp(Growth.Rate*t))
duplicated(colnames(wp))
##  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE

##4.4 Value extraction and plot ##4.4.1.Based on 2022 population, extract top ten countries ##with high population number

library(dplyr)
top10 <- wp %>%
  arrange(desc(X2022.Population)) %>%
  select(Country.Territory,
         X2022.Population) %>%
  head(10)
top10
##    Country.Territory X2022.Population
## 1              China       1425887337
## 2              India       1417173173
## 3      United States        338289857
## 4          Indonesia        275501339
## 5           Pakistan        235824862
## 6            Nigeria        218541212
## 7             Brazil        215313498
## 8         Bangladesh        171186372
## 9             Russia        144713314
## 10            Mexico        127504125
barplot(top10$X2022.Population,names.arg = top10$Country.Territory,
        las=3,main = "top ten countries population",xlab = "",
        ylab = "population")

##4.4.2.trend in their population number since 1990-2022

library(dplyr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.1     ✔ readr     2.2.0
## ✔ ggplot2   4.0.3     ✔ stringr   1.6.0
## ✔ lubridate 1.9.5     ✔ tibble    3.3.1
## ✔ purrr     1.2.2     ✔ tidyr     1.3.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(stringr)

top10_long <- wp %>%
  arrange(desc(X2022.Population))%>%
  select(Country.Territory, X2022.Population, X2015.Population, X2010.Population, 
         X2000.Population, X1990.Population) %>%
  pivot_longer(
    cols = ends_with("Population"), 
    names_to = "Year", 
    values_to = "Population"
  ) %>%
  # Clean the "Year" string (e.g., "X1990.Population" -> 1990)
  mutate(Year = as.numeric(str_extract(Year, "\\d+")))

# Check the result
trend_pop<-head(top10_long,20)
print(trend_pop)
## # A tibble: 20 × 3
##    Country.Territory  Year Population
##    <chr>             <dbl>      <int>
##  1 China              2022 1425887337
##  2 China              2015 1393715448
##  3 China              2010 1348191368
##  4 China              2000 1264099069
##  5 China              1990 1153704252
##  6 India              2022 1417173173
##  7 India              2015 1322866505
##  8 India              2010 1240613620
##  9 India              2000 1059633675
## 10 India              1990  870452165
## 11 United States      2022  338289857
## 12 United States      2015  324607776
## 13 United States      2010  311182845
## 14 United States      2000  282398554
## 15 United States      1990  248083732
## 16 Indonesia          2022  275501339
## 17 Indonesia          2015  259091970
## 18 Indonesia          2010  244016173
## 19 Indonesia          2000  214072421
## 20 Indonesia          1990  182159874
##trend graph
library(ggplot2)
ggplot(trend_pop, aes(x = Year, y = Population, color = Country.Territory)) +
  geom_line(linewidth = 1) +
  geom_point() +
  scale_y_continuous(labels = scales::comma) +
  labs(
    title = "Population Trend of Top 10 Countries (1990-2022)",
    x = "Year",
    y = "Total Population",
    color = "Country"
  ) +
  theme_minimal()

##4.4.3.The most populous countries and ##emission trend since 1990-2019)

library(dplyr)
library(tidyverse)
str(CO2)
## 'data.frame':    215 obs. of  35 variables:
##  $ Country.Name  : chr  "Aruba" "Afghanistan" "Angola" "Albania" ...
##  $ country_code  : chr  "ABW" "AFG" "AGO" "ALB" ...
##  $ Region        : chr  "Latin America & Caribbean" "South Asia" "Sub-Saharan Africa" "Europe & Central Asia" ...
##  $ Indicator.Name: chr  "CO2 emissions (metric tons per capita)" "CO2 emissions (metric tons per capita)" "CO2 emissions (metric tons per capita)" "CO2 emissions (metric tons per capita)" ...
##  $ X1990         : num  NA 0.192 0.554 1.82 7.522 ...
##  $ X1991         : num  NA 0.168 0.545 1.243 7.235 ...
##  $ X1992         : num  NA 0.096 0.544 0.684 6.963 ...
##  $ X1993         : num  NA 0.0847 0.709 0.6383 6.7242 ...
##  $ X1994         : num  NA 0.0755 0.8368 0.6454 6.5416 ...
##  $ X1995         : num  NA 0.0685 0.9121 0.6054 6.7335 ...
##  $ X1996         : num  NA 0.0626 1.0722 0.6124 6.9916 ...
##  $ X1997         : num  NA 0.0568 1.0866 0.4669 7.3074 ...
##  $ X1998         : num  NA 0.0527 1.0918 0.5722 7.6395 ...
##  $ X1999         : num  NA 0.0402 1.1099 0.9554 7.9232 ...
##  $ X2000         : num  NA 0.0366 0.9881 1.0262 7.9523 ...
##  $ X2001         : num  NA 0.0338 0.9418 1.0555 7.7215 ...
##  $ X2002         : num  NA 0.0456 0.8956 1.2324 7.5662 ...
##  $ X2003         : num  NA 0.0515 0.9249 1.339 7.2424 ...
##  $ X2004         : num  NA 0.0417 0.9303 1.4041 7.3443 ...
##  $ X2005         : num  NA 0.0604 0.8135 1.3382 7.3538 ...
##  $ X2006         : num  NA 0.0666 0.8218 1.34 6.7905 ...
##  $ X2007         : num  NA 0.0653 0.8118 1.3939 6.531 ...
##  $ X2008         : num  NA 0.128 0.889 1.384 6.439 ...
##  $ X2009         : num  NA 0.172 0.939 1.441 6.157 ...
##  $ X2010         : num  NA 0.244 0.976 1.528 6.157 ...
##  $ X2011         : num  NA 0.297 0.986 1.669 5.851 ...
##  $ X2012         : num  NA 0.259 0.951 1.503 5.945 ...
##  $ X2013         : num  NA 0.186 1.036 1.534 5.943 ...
##  $ X2014         : num  NA 0.146 1.1 1.668 5.807 ...
##  $ X2015         : num  NA 0.173 1.135 1.604 6.026 ...
##  $ X2016         : num  NA 0.15 1.03 1.56 6.08 ...
##  $ X2017         : num  NA 0.132 0.813 1.789 6.104 ...
##  $ X2018         : num  NA 0.163 0.778 1.783 6.363 ...
##  $ X2019         : num  NA 0.16 0.792 1.692 6.481 ...
##  $ X2019.1       : num  NA 0.16 0.792 1.692 6.481 ...
sum(colSums(is.na(CO2))==nrow(CO2))
## [1] 0
colnames(CO2)
##  [1] "Country.Name"   "country_code"   "Region"         "Indicator.Name"
##  [5] "X1990"          "X1991"          "X1992"          "X1993"         
##  [9] "X1994"          "X1995"          "X1996"          "X1997"         
## [13] "X1998"          "X1999"          "X2000"          "X2001"         
## [17] "X2002"          "X2003"          "X2004"          "X2005"         
## [21] "X2006"          "X2007"          "X2008"          "X2009"         
## [25] "X2010"          "X2011"          "X2012"          "X2013"         
## [29] "X2014"          "X2015"          "X2016"          "X2017"         
## [33] "X2018"          "X2019"          "X2019.1"
dim(CO2)
## [1] 215  35
##trend sice 1990-2019
top10_pop <- trend_pop$Country.Territory
print(top10_pop)
##  [1] "China"         "China"         "China"         "China"        
##  [5] "China"         "India"         "India"         "India"        
##  [9] "India"         "India"         "United States" "United States"
## [13] "United States" "United States" "United States" "Indonesia"    
## [17] "Indonesia"     "Indonesia"     "Indonesia"     "Indonesia"
#extract
top10_pop_co2<-CO2 %>%
filter(Country.Name %in% top10_pop)%>%
pivot_longer(
  cols =starts_with("X"),
  names_to = "Year",
  values_to="CO2")%>%
  mutate(Year = as.numeric(str_extract(Year, "\\d+")))
  
print(top10_pop_co2,n=70)
## # A tibble: 124 × 6
##    Country.Name country_code Region              Indicator.Name       Year   CO2
##    <chr>        <chr>        <chr>               <chr>               <dbl> <dbl>
##  1 China        CHN          East Asia & Pacific CO2 emissions (met…  1990 1.91 
##  2 China        CHN          East Asia & Pacific CO2 emissions (met…  1991 2.00 
##  3 China        CHN          East Asia & Pacific CO2 emissions (met…  1992 2.08 
##  4 China        CHN          East Asia & Pacific CO2 emissions (met…  1993 2.24 
##  5 China        CHN          East Asia & Pacific CO2 emissions (met…  1994 2.32 
##  6 China        CHN          East Asia & Pacific CO2 emissions (met…  1995 2.56 
##  7 China        CHN          East Asia & Pacific CO2 emissions (met…  1996 2.52 
##  8 China        CHN          East Asia & Pacific CO2 emissions (met…  1997 2.55 
##  9 China        CHN          East Asia & Pacific CO2 emissions (met…  1998 2.61 
## 10 China        CHN          East Asia & Pacific CO2 emissions (met…  1999 2.52 
## 11 China        CHN          East Asia & Pacific CO2 emissions (met…  2000 2.65 
## 12 China        CHN          East Asia & Pacific CO2 emissions (met…  2001 2.77 
## 13 China        CHN          East Asia & Pacific CO2 emissions (met…  2002 2.98 
## 14 China        CHN          East Asia & Pacific CO2 emissions (met…  2003 3.43 
## 15 China        CHN          East Asia & Pacific CO2 emissions (met…  2004 3.95 
## 16 China        CHN          East Asia & Pacific CO2 emissions (met…  2005 4.47 
## 17 China        CHN          East Asia & Pacific CO2 emissions (met…  2006 4.91 
## 18 China        CHN          East Asia & Pacific CO2 emissions (met…  2007 5.31 
## 19 China        CHN          East Asia & Pacific CO2 emissions (met…  2008 5.44 
## 20 China        CHN          East Asia & Pacific CO2 emissions (met…  2009 5.80 
## 21 China        CHN          East Asia & Pacific CO2 emissions (met…  2010 6.34 
## 22 China        CHN          East Asia & Pacific CO2 emissions (met…  2011 6.90 
## 23 China        CHN          East Asia & Pacific CO2 emissions (met…  2012 7.05 
## 24 China        CHN          East Asia & Pacific CO2 emissions (met…  2013 7.32 
## 25 China        CHN          East Asia & Pacific CO2 emissions (met…  2014 7.29 
## 26 China        CHN          East Asia & Pacific CO2 emissions (met…  2015 7.15 
## 27 China        CHN          East Asia & Pacific CO2 emissions (met…  2016 7.12 
## 28 China        CHN          East Asia & Pacific CO2 emissions (met…  2017 7.23 
## 29 China        CHN          East Asia & Pacific CO2 emissions (met…  2018 7.49 
## 30 China        CHN          East Asia & Pacific CO2 emissions (met…  2019 7.61 
## 31 China        CHN          East Asia & Pacific CO2 emissions (met…  2019 7.61 
## 32 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1990 0.819
## 33 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1991 0.880
## 34 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1992 0.914
## 35 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1993 0.970
## 36 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1994 1.03 
## 37 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1995 1.14 
## 38 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1996 1.18 
## 39 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1997 1.29 
## 40 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1998 1.28 
## 41 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  1999 1.34 
## 42 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2000 1.33 
## 43 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2001 1.41 
## 44 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2002 1.41 
## 45 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2003 1.52 
## 46 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2004 1.53 
## 47 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2005 1.51 
## 48 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2006 1.59 
## 49 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2007 1.64 
## 50 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2008 1.60 
## 51 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2009 1.64 
## 52 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2010 1.72 
## 53 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2011 1.94 
## 54 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2012 1.94 
## 55 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2013 1.78 
## 56 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2014 1.90 
## 57 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2015 1.89 
## 58 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2016 1.84 
## 59 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2017 1.95 
## 60 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2018 2.16 
## 61 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2019 2.29 
## 62 Indonesia    IDN          East Asia & Pacific CO2 emissions (met…  2019 2.29 
## 63 India        IND          South Asia          CO2 emissions (met…  1990 0.645
## 64 India        IND          South Asia          CO2 emissions (met…  1991 0.681
## 65 India        IND          South Asia          CO2 emissions (met…  1992 0.689
## 66 India        IND          South Asia          CO2 emissions (met…  1993 0.702
## 67 India        IND          South Asia          CO2 emissions (met…  1994 0.725
## 68 India        IND          South Asia          CO2 emissions (met…  1995 0.765
## 69 India        IND          South Asia          CO2 emissions (met…  1996 0.788
## 70 India        IND          South Asia          CO2 emissions (met…  1997 0.819
## # ℹ 54 more rows
sum(duplicated(top10_pop_co2))
## [1] 4

##HW.5.visualize trend by graph

library(ggplot2)
ggplot(top10_pop_co2,aes(x=reorder(Country.Name,CO2),y=CO2))+
  geom_col(fill = "steelblue")+
  coord_flip()+
  scale_y_continuous(labels = scales::comma)+
  labs(title = "CO2 Emission for populous countries",
       x="Country",
       y="CO2",
       color="Country")+
  theme_minimal()
## Ignoring unknown labels:
## • colour : "Country"

##4.5.1.Correlation with heatmap

library(corrplot)
## corrplot 0.95 loaded
df_corr <- read.csv("world_population.csv", check.names = FALSE)
df_co2 <- read.csv("CO2_emission.csv", check.names = FALSE)
head(df_corr)
##   Rank CCA3 Country/Territory          Capital Continent 2022 Population
## 1   36  AFG       Afghanistan            Kabul      Asia        41128771
## 2  138  ALB           Albania           Tirana    Europe         2842321
## 3   34  DZA           Algeria          Algiers    Africa        44903225
## 4  213  ASM    American Samoa        Pago Pago   Oceania           44273
## 5  203  AND           Andorra Andorra la Vella    Europe           79824
## 6   42  AGO            Angola           Luanda    Africa        35588987
##   2020 Population 2015 Population 2010 Population 2000 Population
## 1        38972230        33753499        28189672        19542982
## 2         2866849         2882481         2913399         3182021
## 3        43451666        39543154        35856344        30774621
## 4           46189           51368           54849           58230
## 5           77700           71746           71519           66097
## 6        33428485        28127721        23364185        16394062
##   1990 Population 1980 Population 1970 Population Area (km²) Density (per km²)
## 1        10694796        12486631        10752971     652230           63.0587
## 2         3295066         2941651         2324731      28748           98.8702
## 3        25518074        18739378        13795915    2381741           18.8531
## 4           47818           32886           27075        199          222.4774
## 5           53569           35611           19860        468          170.5641
## 6        11828638         8330047         6029700    1246700           28.5466
##   Growth Rate World Population Percentage
## 1      1.0257                        0.52
## 2      0.9957                        0.04
## 3      1.0164                        0.56
## 4      0.9831                        0.00
## 5      1.0100                        0.00
## 6      1.0315                        0.45
# Select relevant columns
# Use backticks for column names containing spaces or special characters
cols_to_analyze <- df_corr[, c("Area (km²)", 
                          "Density (per km²)", 
                          "Growth Rate", 
                          "World Population Percentage")]

#Calculate the correlation matrix
# 'use = "complete.obs"' handles any missing values by excluding those rows
correlation_matrix <- cor(cols_to_analyze, use = "complete.obs")

#Display numerical values
print("Numerical Correlation Matrix:")
## [1] "Numerical Correlation Matrix:"
print(correlation_matrix)
##                              Area (km²) Density (per km²) Growth Rate
## Area (km²)                   1.00000000       -0.06312785 -0.01397017
## Density (per km²)           -0.06312785        1.00000000 -0.06975328
## Growth Rate                 -0.01397017       -0.06975328  1.00000000
## World Population Percentage  0.45328363       -0.02764600 -0.02092954
##                             World Population Percentage
## Area (km²)                                   0.45328363
## Density (per km²)                           -0.02764600
## Growth Rate                                 -0.02092954
## World Population Percentage                  1.00000000
###Create the heatmap plot
corrplot(correlation_matrix, 
         method = "color",      # Use solid colors for cells
         type = "lower",        # Only show the lower triangle (cuts out redundant mirror data)
         addCoef.col = "black", # Overlay the numerical correlation coefficients
         tl.col = "black",      # Color of text labels (variable names)
         tl.srt = 45,           # Rotate text labels by 45 degrees
         diag = FALSE,          # Hide the diagonal 1.00 correlations to reduce clutter
         title = "Population Metrics Correlation",
         mar = c(0, 0, 2, 0))# Adjust margins so the title doesn't get cut off

##4.5.2.Merge the World Population Dataset and CO2 Emissions

df_pop_sub <- df_corr %>% 
  select(`Country/Territory`, CCA3, Population_2022 = `2022 Population`)
head(df_pop_sub,n=20)
##      Country/Territory CCA3 Population_2022
## 1          Afghanistan  AFG        41128771
## 2              Albania  ALB         2842321
## 3              Algeria  DZA        44903225
## 4       American Samoa  ASM           44273
## 5              Andorra  AND           79824
## 6               Angola  AGO        35588987
## 7             Anguilla  AIA           15857
## 8  Antigua and Barbuda  ATG           93763
## 9            Argentina  ARG        45510318
## 10             Armenia  ARM         2780469
## 11               Aruba  ABW          106445
## 12           Australia  AUS        26177413
## 13             Austria  AUT         8939617
## 14          Azerbaijan  AZE        10358074
## 15             Bahamas  BHS          409984
## 16             Bahrain  BHR         1472233
## 17          Bangladesh  BGD       171186372
## 18            Barbados  BRB          281635
## 19             Belarus  BLR         9534954
## 20             Belgium  BEL        11655930
head(df_co2)
##           Country Name country_code                     Region
## 1                Aruba          ABW  Latin America & Caribbean
## 2          Afghanistan          AFG                 South Asia
## 3               Angola          AGO         Sub-Saharan Africa
## 4              Albania          ALB      Europe & Central Asia
## 5              Andorra          AND      Europe & Central Asia
## 6 United Arab Emirates          ARE Middle East & North Africa
##                           Indicator Name       1990       1991        1992
## 1 CO2 emissions (metric tons per capita)         NA         NA          NA
## 2 CO2 emissions (metric tons per capita)  0.1917451  0.1676816  0.09595774
## 3 CO2 emissions (metric tons per capita)  0.5536620  0.5445386  0.54355722
## 4 CO2 emissions (metric tons per capita)  1.8195416  1.2428102  0.68369983
## 5 CO2 emissions (metric tons per capita)  7.5218317  7.2353792  6.96307870
## 6 CO2 emissions (metric tons per capita) 30.1951886 31.7784962 29.08092584
##          1993        1994        1995        1996        1997        1998
## 1          NA          NA          NA          NA          NA          NA
## 2  0.08472111  0.07554583  0.06846796  0.06258803  0.05682662  0.05269086
## 3  0.70898423  0.83680440  0.91214149  1.07216847  1.08663697  1.09182531
## 4  0.63830704  0.64535519  0.60543625  0.61236736  0.46692147  0.57215370
## 5  6.72417752  6.54157891  6.73347949  6.99159455  7.30744115  7.63953851
## 6 29.27567777 30.84933296 31.12501806 30.92802588 30.48633262 29.66358052
##          1999       2000        2001        2002        2003        2004
## 1          NA         NA          NA          NA          NA          NA
## 2  0.04015697  0.0365737  0.03378536  0.04557366  0.05151838  0.04165539
## 3  1.10985966  0.9880774  0.94182891  0.89557767  0.92486944  0.93026295
## 4  0.95535931  1.0262131  1.05549588  1.23237878  1.33898498  1.40405869
## 5  7.92319165  7.9522863  7.72154906  7.56623988  7.24241557  7.34426233
## 6 28.88710798 27.0351591 29.43026994 28.50146173 27.96926982 27.03893822
##          2005        2006        2007       2008       2009       2010
## 1          NA          NA          NA         NA         NA         NA
## 2  0.06041878  0.06658329  0.06531235  0.1284166  0.1718624  0.2436140
## 3  0.81353929  0.82184008  0.81175351  0.8886580  0.9394040  0.9761842
## 4  1.33820940  1.33999574  1.39393137  1.3843112  1.4414936  1.5276237
## 5  7.35378001  6.79054277  6.53104692  6.4393039  6.1566875  6.1571978
## 6 25.38238104 22.93510429 21.37028576 22.0114692 19.8323489 19.0397698
##         2011       2012       2013       2014       2015       2016       2017
## 1         NA         NA         NA         NA         NA         NA         NA
## 2  0.2965062  0.2592953  0.1856237  0.1462356  0.1728967  0.1497893  0.1316946
## 3  0.9855223  0.9506959  1.0362939  1.0997791  1.1350441  1.0318113  0.8133007
## 4  1.6694232  1.5032405  1.5336300  1.6683374  1.6037751  1.5576644  1.7887861
## 5  5.8508861  5.9446542  5.9428004  5.8071277  6.0261818  6.0806003  6.1041339
## 6 18.5094574 19.2078011 20.0556476 20.0516980 21.0776420 21.4806686 20.7690223
##         2018       2019       2019
## 1         NA         NA         NA
## 2  0.1632953  0.1598244  0.1598244
## 3  0.7776749  0.7921371  0.7921371
## 4  1.7827389  1.6922483  1.6922483
## 5  6.3629754  6.4812174  6.4812174
## 6 18.3906781 19.3295633 19.3295633
colnames(df_co2) <- make.unique(colnames(df_co2))
df_co2_sub <- df_co2 %>% 
  select(country_code, CO2_Per_Capita_2019 = `2019`)
head(df_co2_sub,n=20)
##    country_code CO2_Per_Capita_2019
## 1           ABW                  NA
## 2           AFG          0.15982437
## 3           AGO          0.79213707
## 4           ALB          1.69224832
## 5           AND          6.48121743
## 6           ARE         19.32956328
## 7           ARG          3.74065029
## 8           ARM          2.08606068
## 9           ASM                  NA
## 10          ATG          5.35447646
## 11          AUS         15.23826715
## 12          AUT          7.29398425
## 13          AZE          3.54239783
## 14          BDI          0.06244267
## 15          BEL          8.09558395
## 16          BEN          0.61858375
## 17          BFA          0.24604625
## 18          BGD          0.55652945
## 19          BGR          5.61085728
## 20          BHR         20.26610279

##merge two dataset HW2 ##INNER JOIN is exclusive:It only keeps rows where there is a match in both tables. ##OUTER JOIN is inclusive:It keeps matched rows plus unmatched rows from one or both tables (filling in the missing gaps with NULL). #so here we use INNER join because we need match=“country_code”on both tables

merged_df <- inner_join(df_pop_sub, df_co2_sub, by = c("CCA3" = "country_code"))
head(merged_df,n=20)
##      Country/Territory CCA3 Population_2022 CO2_Per_Capita_2019
## 1          Afghanistan  AFG        41128771           0.1598244
## 2              Albania  ALB         2842321           1.6922483
## 3              Algeria  DZA        44903225           3.9776505
## 4       American Samoa  ASM           44273                  NA
## 5              Andorra  AND           79824           6.4812174
## 6               Angola  AGO        35588987           0.7921371
## 7  Antigua and Barbuda  ATG           93763           5.3544765
## 8            Argentina  ARG        45510318           3.7406503
## 9              Armenia  ARM         2780469           2.0860607
## 10               Aruba  ABW          106445                  NA
## 11           Australia  AUS        26177413          15.2382672
## 12             Austria  AUT         8939617           7.2939843
## 13          Azerbaijan  AZE        10358074           3.5423978
## 14             Bahamas  BHS          409984           7.2916611
## 15             Bahrain  BHR         1472233          20.2661028
## 16          Bangladesh  BGD       171186372           0.5565295
## 17            Barbados  BRB          281635           4.3550820
## 18             Belarus  BLR         9534954           6.1222378
## 19             Belgium  BEL        11655930           8.0955840
## 20              Belize  BLZ          405272           1.6395500

Calculate the Total National CO2 Emissions

merged_df <- merged_df %>% 
  mutate(Total_CO2_2019 = CO2_Per_Capita_2019 * Population_2022)
head(merged_df,n=20)
##      Country/Territory CCA3 Population_2022 CO2_Per_Capita_2019 Total_CO2_2019
## 1          Afghanistan  AFG        41128771           0.1598244      6573380.0
## 2              Albania  ALB         2842321           1.6922483      4809912.9
## 3              Algeria  DZA        44903225           3.9776505    178609333.5
## 4       American Samoa  ASM           44273                  NA             NA
## 5              Andorra  AND           79824           6.4812174       517356.7
## 6               Angola  AGO        35588987           0.7921371     28191355.9
## 7  Antigua and Barbuda  ATG           93763           5.3544765       502051.8
## 8            Argentina  ARG        45510318           3.7406503    170238184.3
## 9              Armenia  ARM         2780469           2.0860607      5800227.0
## 10               Aruba  ABW          106445                  NA             NA
## 11           Australia  AUS        26177413          15.2382672    398898412.6
## 12             Austria  AUT         8939617           7.2939843     65205425.6
## 13          Azerbaijan  AZE        10358074           3.5423978     36692418.8
## 14             Bahamas  BHS          409984           7.2916611      2989464.4
## 15             Bahrain  BHR         1472233          20.2661028     29836425.3
## 16          Bangladesh  BGD       171186372           0.5565295     95270258.3
## 17            Barbados  BRB          281635           4.3550820      1226543.5
## 18             Belarus  BLR         9534954           6.1222378     58375255.5
## 19             Belgium  BEL        11655930           8.0955840     94361559.9
## 20              Belize  BLZ          405272           1.6395500       664463.7
# Statistical Analysis (Correlations)
# Population vs Per Capita CO2
pearson_per_capita <- cor(merged_df$Population_2022, 
                          merged_df$CO2_Per_Capita_2019, 
                          method = "pearson")

spearman_per_capita <- cor(merged_df$Population_2022, 
                           merged_df$CO2_Per_Capita_2019, 
                           method = "spearman")
# Population vs Total CO2
pearson_total <- cor(merged_df$Population_2022, 
                     merged_df$Total_CO2_2019, 
                     method = "pearson")

spearman_total <- cor(merged_df$Population_2022,
                      merged_df$Total_CO2_2019, 
                      method = "spearman")
cat(sprintf("Population vs Per Capita CO2 -> 
            Pearson: %.4f, Spearman: %.4f\n", 
            pearson_per_capita, 
            spearman_per_capita))
## Population vs Per Capita CO2 -> 
##             Pearson: NA, Spearman: NA
cat(sprintf("Population vs Total CO2 ->
            Pearson: %.4f, Spearman: %.4f\n",
            pearson_total, spearman_total))
## Population vs Total CO2 ->
##             Pearson: NA, Spearman: NA
#to decide if i need to apply logarithm on my data
hist(merged_df$Population_2022)  # Is it heavily bunched up 

##on the left with a long tail to the right?
plot(merged_df$Population_2022, merged_df$Total_CO2_2019) 

# Is it a straight line, or a curve/fan?
# 5. Regression Analysis (Log-Log Model)
# Filter out 0 values before log transformation to avoid -Inf errors
merged_df_log <- merged_df %>% 
  filter(Total_CO2_2019 > 0 & CO2_Per_Capita_2019 > 0) %>% 
  mutate(
    Log_Population = log10(Population_2022),
    Log_Total_CO2 = log10(Total_CO2_2019)
  )

# Fit the Linear Model: log10(Total_CO2) = beta_0 + beta_1 * log10(Population)
log_model <- lm(Log_Total_CO2 ~ Log_Population, data = merged_df_log)

# Display regression statistics (R², slope, p-values)
print(summary(log_model))
## 
## Call:
## lm(formula = Log_Total_CO2 ~ Log_Population, data = merged_df_log)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.6300 -0.3920  0.1216  0.4493  1.1769 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     0.89576    0.32144   2.787  0.00587 ** 
## Log_Population  0.91274    0.04666  19.563  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6091 on 189 degrees of freedom
## Multiple R-squared:  0.6694, Adjusted R-squared:  0.6677 
## F-statistic: 382.7 on 1 and 189 DF,  p-value: < 2.2e-16
# 6. Visualizations

# Plot 1: Population vs Per-Capita CO2 (Linear scale)
plot1 <- ggplot(merged_df, aes(x = Population_2022, y = CO2_Per_Capita_2019)) +
  geom_point(alpha = 0.7) +
  geom_smooth(method = "lm", color = "red", linetype = "dashed", se = FALSE) +
  labs(
    title = "Population vs. Per Capita CO2 Emissions (2019)",
    x = "2022 Population (Linear Scale)",
    y = "CO2 Emissions (metric tons per capita)"
  ) +
  theme_minimal()

print(plot1)
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 23 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 23 rows containing missing values or values outside the scale range
## (`geom_point()`).

##HW6.>to create my own function of doing summarystatistics

get_summary_stat<-function(df) {
  
  # 1. Handle Numeric Columns
  numeric_summary <- df %>%
    select(where(is.numeric)) %>%
    pivot_longer(cols = everything(), names_to = "variable", values_to = "value") %>%
    group_by(variable) %>%
    summarise(
      total_count   = n(),
      missing_count = sum(is.na(value)),
      mean          = mean(value, na.rm = TRUE),
      sd            = sd(value, na.rm = TRUE),
      variance      = var(value, na.rm = TRUE),
      min           = min(value, na.rm = TRUE),
      q1           = quantile(value, 0.25, na.rm = TRUE),
      median        = median(value, na.rm = TRUE),
      q2           = quantile(value, 0.75, na.rm = TRUE),
      max           = max(value, na.rm = TRUE),
      .groups = "drop"
    )
  
  # 2. Handle Categorical Columns (Factors and Characters)
  categorical_summary <- df %>%
    select(where(is.character) | where(is.factor)) %>%
    pivot_longer(cols = everything(), names_to = "variable", values_to = "value") %>%
    # Convert value to character so factors and strings play nice together
    mutate(value = as.character(value)) %>% 
    group_by(variable) %>%
    summarise(
      total_count   = n(),
      missing_count = sum(is.na(value)),
      unique_count  = n_distinct(value, na.rm = TRUE),
      # Find the most frequent value (mode)
      most_frequent = if(all(is.na(value))) NA_character_ else names(sort(table(value), decreasing = TRUE))[1],
      .groups = "drop"
    )
  
  # Return both summaries as a named list
  return(list(
    numeric = numeric_summary,
    categorical = categorical_summary
  ))
}
  
 summary_f<- get_summary_stat(wp)
 summary_f$numeric
## # A tibble: 14 × 11
##    variable total_count missing_count     mean       sd variance     min      q1
##    <chr>          <int>         <int>    <dbl>    <dbl>    <dbl>   <dbl>   <dbl>
##  1 Area..k…         234             0 5.81e+ 5 1.76e+ 6 3.10e+12 1   e+0 2.65e+3
##  2 Density…         234             0 4.52e+ 2 2.07e+ 3 4.27e+ 6 2.61e-2 3.84e+1
##  3 Growth.…         234             0 1.01e+ 0 1.34e- 2 1.79e- 4 9.12e-1 1.00e+0
##  4 Rank             234             0 1.17e+ 2 6.77e+ 1 4.58e+ 3 1   e+0 5.92e+1
##  5 World.P…         234             0 4.27e- 1 1.71e+ 0 2.94e+ 0 0       1   e-2
##  6 X1970.P…         234             0 1.58e+ 7 6.78e+ 7 4.60e+15 7.52e+2 1.56e+5
##  7 X1980.P…         234             0 1.90e+ 7 8.18e+ 7 6.69e+15 7.33e+2 2.30e+5
##  8 X1990.P…         234             0 2.27e+ 7 9.78e+ 7 9.57e+15 7   e+2 2.64e+5
##  9 X2000.P…         234             0 2.63e+ 7 1.12e+ 8 1.25e+16 6.51e+2 3.27e+5
## 10 X2010.P…         234             0 2.98e+ 7 1.24e+ 8 1.54e+16 5.96e+2 3.93e+5
## 11 X2015.P…         234             0 3.17e+ 7 1.30e+ 8 1.70e+16 5.64e+2 4.05e+5
## 12 X2020.P…         234             0 3.35e+ 7 1.36e+ 8 1.84e+16 5.2 e+2 4.15e+5
## 13 X2022.P…         234             0 3.41e+ 7 1.37e+ 8 1.87e+16 5.1 e+2 4.20e+5
## 14 X2030.p…         234             0 1.09e+11 4.21e+11 1.78e+23 1.50e+6 1.36e+9
## # ℹ 3 more variables: median <dbl>, q2 <dbl>, max <dbl>
 summary_f$categorical
## # A tibble: 4 × 5
##   variable          total_count missing_count unique_count most_frequent
##   <chr>                   <int>         <int>        <int> <chr>        
## 1 CCA3                      234             0          234 ABW          
## 2 Capital                   234             0          234 Abu Dhabi    
## 3 Continent                 234             0            6 Africa       
## 4 Country.Territory         234             0          234 Afghanistan

#HW7.use of trace() and recover() ##trace():“modify or inspect a function while it runs” ##recover(): “investigate an error after it happens