Descriptive Analysis Question

Q5 - What are the minimum, median, maximum, inter quartile range, and outlier values for immigrants to Canada from 1980 to 2013 per decade. Visualized by box plot

Import the necessary libraries

library(tidyr) #For data manipulation and ggplot
library(dplyr) #For data manipulation
## 
## 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
library(ggpubr) #For sub plotting
## Loading required package: ggplot2
DF <- read.csv('/Users/salahkaf/Desktop/UpdatedDF.csv') #Reading the DF

Wrangling the table part 1

total_decade80s = vector(mode = "list") # creating empty lists to append the values for each year using for loop

total_decade90s = vector(mode = "list")
total_decade2000s = vector(mode = "list")
Years20s <- for (country in DF$Country) {
  a <- select(filter(DF, Country == country), X2000:X2009 )
  total <- apply(a, 1, sum)
  total_decade2000s <- append(total_decade2000s, total)
}

Years90s <- for (country in DF$Country) {
  a <- select(filter(DF, Country == country), X1990:X1999 )
  total <- apply(a, 1, sum)
  total_decade90s <- append(total_decade90s, total)
}

Years80s <- for (country in DF$Country) {
  a <- select(filter(DF, Country == country), X1980:X1989 )
  total <- apply(a, 1, sum)
  total_decade80s <- append(total_decade80s, total)
}

Wrangling the table part 2

total_decade <- as.data.frame(cbind(DF$Country, total_decade80s,total_decade90s,total_decade2000s)) # making a data frame using the appended lists
total_decade <- total_decade[-nrow(total_decade),] #deleting the total row
names(total_decade) <- c("Country", "1980s", "1990s", "2000s") 
total_decade$`1980s` <- as.integer(total_decade$`1980s`) # changing the values of each column to integer 
total_decade$`1990s` <- as.integer(total_decade$`1990s`)
total_decade$`2000s` <- as.integer(total_decade$`2000s`)
total_decade
##                                                  Country  1980s  1990s  2000s
## 1                                            Afghanistan   3693  15845  30501
## 2                                                Albania      9   2568  10799
## 3                                                Algeria   1271  13153  37833
## 4                                         American Samoa      3      2      1
## 5                                                Andorra      2      6      5
## 6                                                 Angola     50    285   1563
## 7                                    Antigua and Barbuda    291    315    235
## 8                                              Argentina   3738   5686   8890
## 9                                                Armenia      0    602   1755
## 10                                             Australia   4564   6574   8804
## 11                                               Austria   1968   1546   1071
## 12                                            Azerbaijan      0    238   1846
## 13                                               Bahamas    243    195    190
## 14                                               Bahrain     29    145    181
## 15                                            Bangladesh   2376  18532  30816
## 16                                              Barbados   2935   2388   1153
## 17                                               Belarus      0   1755   4901
## 18                                               Belgium   3351   2430   3505
## 19                                                Belize    274    344    303
## 20                                                 Benin     65    226   1199
## 21                                                Bhutan      2     18    951
## 22                      Bolivia (Plurinational State of)    644   1062   1043
## 23                                Bosnia and Herzegovina      0  17789   3518
## 24                                              Botswana     49     21     91
## 25                                                Brazil   2530   6931  12736
## 26                                     Brunei Darussalam    295    221     57
## 27                                              Bulgaria    337   7481  13608
## 28                                          Burkina Faso     48    154    920
## 29                                               Burundi     24   1085   4704
## 30                                            Cabo Verde     36    104     47
## 31                                              Cambodia    171   2175   3275
## 32                                              Cameroon    142   1137   6358
## 33                                                Canada      0     16      0
## 34                              Central African Republic     17     69    206
## 35                                                  Chad     28    260   1005
## 36                                                 Chile   9475   6874   3932
## 37                                                 China  32003 161528 340385
## 38        China, Hong Kong Special Administrative Region      0    681   5930
## 39            China, Macao Special Administrative Region      0      0    188
## 40                                              Colombia   3069   6252  45811
## 41                                               Comoros     11     26     81
## 42                                                 Congo     17    237   1071
## 43                                            Costa Rica    735   1283   1827
## 44                                         Côte d'Ivoire    179    738   3478
## 45                                               Croatia      0   5260   2278
## 46                                                  Cuba   1214   3969  10553
## 47                                                Cyprus    686    276    112
## 48                                        Czech Republic      0   1452   2097
## 49                 Democratic People's Republic of Korea     13     31    119
## 50                      Democratic Republic of the Congo    302   5184  13869
## 51                                               Denmark   1596   1074    871
## 52                                              Djibouti     24    266    741
## 53                                              Dominica    814    826    660
## 54                                    Dominican Republic   1986   3846   2949
## 55                                               Ecuador   2298   4398   4136
## 56                                                 Egypt   7556  20040  24784
## 57                                           El Salvador  21648  24334   6138
## 58                                     Equatorial Guinea      1      8     29
## 59                                               Eritrea      0    427   4856
## 60                                               Estonia      0    474    347
## 61                                              Ethiopia   3263  10766  13609
## 62                                                  Fiji   5721   9397   4279
## 63                                               Finland   1132    743    633
## 64                                                France  17137  30028  41297
## 65                                                 Gabon     22     90    549
## 66                                                Gambia     38    244    209
## 67                                               Georgia      0    357   1174
## 68                                               Germany  18217  14186  23581
## 69                                                 Ghana   2898  13480   7276
## 70                                                Greece   7361   3263   1139
## 71                                               Grenada   1938   3342   3118
## 72                                             Guatemala   6074   9693   2441
## 73                                                Guinea     67    493   2714
## 74                                         Guinea-Bissau      8     23     44
## 75                                                Guyana  32096  27260  13340
## 76                                                 Haiti  22546  21710  19290
## 77                                              Honduras    756   3691   1613
## 78                                               Hungary   5522   5036   4898
## 79                                               Iceland    120    127    141
## 80                                                 India  82154 180395 303591
## 81                                             Indonesia   1824   2554   6888
## 82                            Iran (Islamic Republic of)  21477  54871  65794
## 83                                                  Iraq   3985  18140  26568
## 84                                               Ireland   7647   3909   2596
## 85                                                Israel  11975  20792  24937
## 86                                                 Italy  11487   5734   3610
## 87                                               Jamaica  34328  40329  22733
## 88                                                 Japan   4296   8077  10705
## 89                                                Jordan   2733  10481  16265
## 90                                            Kazakhstan      0   2291   4631
## 91                                                 Kenya   4468   4434   5845
## 92                                              Kiribati      3      5      5
## 93                                                Kuwait     67    987    725
## 94                                            Kyrgyzstan      0    207   1429
## 95                      Lao People's Democratic Republic    180    368    425
## 96                                                Latvia      1    686   1051
## 97                                               Lebanon  24918  49245  30906
## 98                                               Lesotho     32     37     24
## 99                                               Liberia     31    384   1979
## 100                                                Libya    310   1051   2258
## 101                                        Liechtenstein     13     18      6
## 102                                            Lithuania      5    680   1398
## 103                                           Luxembourg     49     53     81
## 104                                           Madagascar    240    373    861
## 105                                               Malawi     36     85    110
## 106                                             Malaysia   9281   8172   5191
## 107                                             Maldives      1      7     13
## 108                                                 Mali     44    422   1016
## 109                                                Malta   1111    193    204
## 110                                     Marshall Islands      0      0      2
## 111                                           Mauritania     27     71    656
## 112                                            Mauritius   3292   2286   5509
## 113                                               Mexico   6131  12170  24376
## 114                                               Monaco      3      9      8
## 115                                             Mongolia      0     15    498
## 116                                           Montenegro      0      0     37
## 117                                              Morocco   4792  11695  38000
## 118                                           Mozambique     27     50     57
## 119                                              Myanmar    466   1370   6030
## 120                                              Namibia     26     26    122
## 121                                                Nauru      2      1     14
## 122                                                Nepal     51    769   4388
## 123                                          Netherlands  10228   6857   7555
## 124                                        New Caledonia      0      0      3
## 125                                          New Zealand   2719   2344   3626
## 126                                            Nicaragua   4132   6581    963
## 127                                                Niger      7     86    489
## 128                                              Nigeria    993   5052  19063
## 129                                               Norway    720    718    682
## 130                                                 Oman     12     50    114
## 131                                             Pakistan  10591  65302 127598
## 132                                                Palau      0      0      1
## 133                                               Panama    244   2173    553
## 134                                     Papua New Guinea     17     16     22
## 135                                             Paraguay    434    488    729
## 136                                                 Peru   6424  10206  12384
## 137                                          Philippines  60764 138482 172904
## 138                                               Poland  57602  64864  13629
## 139                                             Portugal  40432  26904   4484
## 140                                                Qatar      2     43     71
## 141                                    Republic of Korea  16259  38189  68183
## 142                                  Republic of Moldova      0   1527   7852
## 143                                              Romania   7613  33659  45515
## 144                                   Russian Federation      0  19013  35535
## 145                                               Rwanda     62   1360   3404
## 146                                Saint Kitts and Nevis    225    245    110
## 147                                          Saint Lucia    848    994   1676
## 148                     Saint Vincent and the Grenadines   1724   2487   3550
## 149                                                Samoa     23     25     20
## 150                                           San Marino      2      1      1
## 151                                Sao Tome and Principe      0      2      5
## 152                                         Saudi Arabia     90    585   1589
## 153                                              Senegal    135    809   3146
## 154                                               Serbia      0      0    677
## 155                                           Seychelles    117    998    120
## 156                                         Sierra Leone    158    461   2438
## 157                                            Singapore   3969   4612   4687
## 158                                             Slovakia      0   1617   3836
## 159                                             Slovenia      0    237    205
## 160                                              Somalia    905  23103  11428
## 161                                         South Africa   8679  14019  13190
## 162                                                Spain   1740   1100   1232
## 163                                            Sri Lanka  14796  70421  49678
## 164                                   State of Palestine      0    360   3948
## 165                                                Sudan    357   4950  11269
## 166                                             Suriname    172    352    171
## 167                                            Swaziland     36     32     48
## 168                                               Sweden   2011   1652   1630
## 169                                          Switzerland   5151   6198   3334
## 170                                 Syrian Arab Republic   5799  11340  10643
## 171                                           Tajikistan      0     36    295
## 172                                             Thailand    954   2091   4538
## 173            The former Yugoslav Republic of Macedonia      0    978   3103
## 174                                                 Togo     82    416   1817
## 175                                                Tonga     41     60     26
## 176                                  Trinidad and Tobago  12851  25719   8930
## 177                                              Tunisia    840   2718   8154
## 178                                               Turkey   4369   8680  14186
## 179                                         Turkmenistan      0     24    202
## 180                                               Tuvalu      3      1      3
## 181                                               Uganda    362    707   1524
## 182                                              Ukraine      0  14644  26340
## 183                                 United Arab Emirates     34    198    358
## 184 United Kingdom of Great Britain and Northern Ireland 179171 261966  83413
## 185                          United Republic of Tanzania   5630   3490   2555
## 186                             United States of America  76824  56915  75173
## 187                                              Uruguay   1342   1833   1261
## 188                                           Uzbekistan      0    459   2056
## 189                                              Vanuatu      0      5      6
## 190                   Venezuela (Bolivarian Republic of)   1816   4826   9780
## 191                                             Viet Nam  30638  37726  21274
## 192                                       Western Sahara      0      1      1
## 193                                                Yemen     72    756   1395
## 194                                               Zambia    221    548    632
## 195                                             Zimbabwe    790    667   5369
## 196                                              Unknown 201844 249985  53780
total_decade <- filter(total_decade , `1980s`<1000 & `1990s`<1000 & `2000s`<2500 ) #filtering the data to avoid outliers 
total_decade <- gather(total_decade, Decades, value, 2:4) #reshaping the data to a from suitable for box plotting
total_decade
##                                        Country Decades value
## 1                               American Samoa   1980s     3
## 2                                      Andorra   1980s     2
## 3                                       Angola   1980s    50
## 4                          Antigua and Barbuda   1980s   291
## 5                                      Armenia   1980s     0
## 6                                   Azerbaijan   1980s     0
## 7                                      Bahamas   1980s   243
## 8                                      Bahrain   1980s    29
## 9                                       Belize   1980s   274
## 10                                       Benin   1980s    65
## 11                                      Bhutan   1980s     2
## 12                                    Botswana   1980s    49
## 13                           Brunei Darussalam   1980s   295
## 14                                Burkina Faso   1980s    48
## 15                                  Cabo Verde   1980s    36
## 16                                      Canada   1980s     0
## 17                    Central African Republic   1980s    17
## 18                                        Chad   1980s    28
## 19  China, Macao Special Administrative Region   1980s     0
## 20                                     Comoros   1980s    11
## 21                                       Congo   1980s    17
## 22                                      Cyprus   1980s   686
## 23       Democratic People's Republic of Korea   1980s    13
## 24                                    Djibouti   1980s    24
## 25                                    Dominica   1980s   814
## 26                           Equatorial Guinea   1980s     1
## 27                                     Estonia   1980s     0
## 28                                       Gabon   1980s    22
## 29                                      Gambia   1980s    38
## 30                                     Georgia   1980s     0
## 31                               Guinea-Bissau   1980s     8
## 32                                     Iceland   1980s   120
## 33                                    Kiribati   1980s     3
## 34                                      Kuwait   1980s    67
## 35                                  Kyrgyzstan   1980s     0
## 36            Lao People's Democratic Republic   1980s   180
## 37                                      Latvia   1980s     1
## 38                                     Lesotho   1980s    32
## 39                                     Liberia   1980s    31
## 40                               Liechtenstein   1980s    13
## 41                                   Lithuania   1980s     5
## 42                                  Luxembourg   1980s    49
## 43                                  Madagascar   1980s   240
## 44                                      Malawi   1980s    36
## 45                                    Maldives   1980s     1
## 46                                        Mali   1980s    44
## 47                            Marshall Islands   1980s     0
## 48                                  Mauritania   1980s    27
## 49                                      Monaco   1980s     3
## 50                                    Mongolia   1980s     0
## 51                                  Montenegro   1980s     0
## 52                                  Mozambique   1980s    27
## 53                                     Namibia   1980s    26
## 54                                       Nauru   1980s     2
## 55                               New Caledonia   1980s     0
## 56                                       Niger   1980s     7
## 57                                      Norway   1980s   720
## 58                                        Oman   1980s    12
## 59                                       Palau   1980s     0
## 60                            Papua New Guinea   1980s    17
## 61                                    Paraguay   1980s   434
## 62                                       Qatar   1980s     2
## 63                       Saint Kitts and Nevis   1980s   225
## 64                                 Saint Lucia   1980s   848
## 65                                       Samoa   1980s    23
## 66                                  San Marino   1980s     2
## 67                       Sao Tome and Principe   1980s     0
## 68                                Saudi Arabia   1980s    90
## 69                                      Serbia   1980s     0
## 70                                  Seychelles   1980s   117
## 71                                Sierra Leone   1980s   158
## 72                                    Slovenia   1980s     0
## 73                                    Suriname   1980s   172
## 74                                   Swaziland   1980s    36
## 75                                  Tajikistan   1980s     0
## 76                                        Togo   1980s    82
## 77                                       Tonga   1980s    41
## 78                                Turkmenistan   1980s     0
## 79                                      Tuvalu   1980s     3
## 80                                      Uganda   1980s   362
## 81                        United Arab Emirates   1980s    34
## 82                                  Uzbekistan   1980s     0
## 83                                     Vanuatu   1980s     0
## 84                              Western Sahara   1980s     0
## 85                                       Yemen   1980s    72
## 86                                      Zambia   1980s   221
## 87                              American Samoa   1990s     2
## 88                                     Andorra   1990s     6
## 89                                      Angola   1990s   285
## 90                         Antigua and Barbuda   1990s   315
## 91                                     Armenia   1990s   602
## 92                                  Azerbaijan   1990s   238
## 93                                     Bahamas   1990s   195
## 94                                     Bahrain   1990s   145
## 95                                      Belize   1990s   344
## 96                                       Benin   1990s   226
## 97                                      Bhutan   1990s    18
## 98                                    Botswana   1990s    21
## 99                           Brunei Darussalam   1990s   221
## 100                               Burkina Faso   1990s   154
## 101                                 Cabo Verde   1990s   104
## 102                                     Canada   1990s    16
## 103                   Central African Republic   1990s    69
## 104                                       Chad   1990s   260
## 105 China, Macao Special Administrative Region   1990s     0
## 106                                    Comoros   1990s    26
## 107                                      Congo   1990s   237
## 108                                     Cyprus   1990s   276
## 109      Democratic People's Republic of Korea   1990s    31
## 110                                   Djibouti   1990s   266
## 111                                   Dominica   1990s   826
## 112                          Equatorial Guinea   1990s     8
## 113                                    Estonia   1990s   474
## 114                                      Gabon   1990s    90
## 115                                     Gambia   1990s   244
## 116                                    Georgia   1990s   357
## 117                              Guinea-Bissau   1990s    23
## 118                                    Iceland   1990s   127
## 119                                   Kiribati   1990s     5
## 120                                     Kuwait   1990s   987
## 121                                 Kyrgyzstan   1990s   207
## 122           Lao People's Democratic Republic   1990s   368
## 123                                     Latvia   1990s   686
## 124                                    Lesotho   1990s    37
## 125                                    Liberia   1990s   384
## 126                              Liechtenstein   1990s    18
## 127                                  Lithuania   1990s   680
## 128                                 Luxembourg   1990s    53
## 129                                 Madagascar   1990s   373
## 130                                     Malawi   1990s    85
## 131                                   Maldives   1990s     7
## 132                                       Mali   1990s   422
## 133                           Marshall Islands   1990s     0
## 134                                 Mauritania   1990s    71
## 135                                     Monaco   1990s     9
## 136                                   Mongolia   1990s    15
## 137                                 Montenegro   1990s     0
## 138                                 Mozambique   1990s    50
## 139                                    Namibia   1990s    26
## 140                                      Nauru   1990s     1
## 141                              New Caledonia   1990s     0
## 142                                      Niger   1990s    86
## 143                                     Norway   1990s   718
## 144                                       Oman   1990s    50
## 145                                      Palau   1990s     0
## 146                           Papua New Guinea   1990s    16
## 147                                   Paraguay   1990s   488
## 148                                      Qatar   1990s    43
## 149                      Saint Kitts and Nevis   1990s   245
## 150                                Saint Lucia   1990s   994
## 151                                      Samoa   1990s    25
## 152                                 San Marino   1990s     1
## 153                      Sao Tome and Principe   1990s     2
## 154                               Saudi Arabia   1990s   585
## 155                                     Serbia   1990s     0
## 156                                 Seychelles   1990s   998
## 157                               Sierra Leone   1990s   461
## 158                                   Slovenia   1990s   237
## 159                                   Suriname   1990s   352
## 160                                  Swaziland   1990s    32
## 161                                 Tajikistan   1990s    36
## 162                                       Togo   1990s   416
## 163                                      Tonga   1990s    60
## 164                               Turkmenistan   1990s    24
## 165                                     Tuvalu   1990s     1
## 166                                     Uganda   1990s   707
## 167                       United Arab Emirates   1990s   198
## 168                                 Uzbekistan   1990s   459
## 169                                    Vanuatu   1990s     5
## 170                             Western Sahara   1990s     1
## 171                                      Yemen   1990s   756
## 172                                     Zambia   1990s   548
## 173                             American Samoa   2000s     1
## 174                                    Andorra   2000s     5
## 175                                     Angola   2000s  1563
## 176                        Antigua and Barbuda   2000s   235
## 177                                    Armenia   2000s  1755
## 178                                 Azerbaijan   2000s  1846
## 179                                    Bahamas   2000s   190
## 180                                    Bahrain   2000s   181
## 181                                     Belize   2000s   303
## 182                                      Benin   2000s  1199
## 183                                     Bhutan   2000s   951
## 184                                   Botswana   2000s    91
## 185                          Brunei Darussalam   2000s    57
## 186                               Burkina Faso   2000s   920
## 187                                 Cabo Verde   2000s    47
## 188                                     Canada   2000s     0
## 189                   Central African Republic   2000s   206
## 190                                       Chad   2000s  1005
## 191 China, Macao Special Administrative Region   2000s   188
## 192                                    Comoros   2000s    81
## 193                                      Congo   2000s  1071
## 194                                     Cyprus   2000s   112
## 195      Democratic People's Republic of Korea   2000s   119
## 196                                   Djibouti   2000s   741
## 197                                   Dominica   2000s   660
## 198                          Equatorial Guinea   2000s    29
## 199                                    Estonia   2000s   347
## 200                                      Gabon   2000s   549
## 201                                     Gambia   2000s   209
## 202                                    Georgia   2000s  1174
## 203                              Guinea-Bissau   2000s    44
## 204                                    Iceland   2000s   141
## 205                                   Kiribati   2000s     5
## 206                                     Kuwait   2000s   725
## 207                                 Kyrgyzstan   2000s  1429
## 208           Lao People's Democratic Republic   2000s   425
## 209                                     Latvia   2000s  1051
## 210                                    Lesotho   2000s    24
## 211                                    Liberia   2000s  1979
## 212                              Liechtenstein   2000s     6
## 213                                  Lithuania   2000s  1398
## 214                                 Luxembourg   2000s    81
## 215                                 Madagascar   2000s   861
## 216                                     Malawi   2000s   110
## 217                                   Maldives   2000s    13
## 218                                       Mali   2000s  1016
## 219                           Marshall Islands   2000s     2
## 220                                 Mauritania   2000s   656
## 221                                     Monaco   2000s     8
## 222                                   Mongolia   2000s   498
## 223                                 Montenegro   2000s    37
## 224                                 Mozambique   2000s    57
## 225                                    Namibia   2000s   122
## 226                                      Nauru   2000s    14
## 227                              New Caledonia   2000s     3
## 228                                      Niger   2000s   489
## 229                                     Norway   2000s   682
## 230                                       Oman   2000s   114
## 231                                      Palau   2000s     1
## 232                           Papua New Guinea   2000s    22
## 233                                   Paraguay   2000s   729
## 234                                      Qatar   2000s    71
## 235                      Saint Kitts and Nevis   2000s   110
## 236                                Saint Lucia   2000s  1676
## 237                                      Samoa   2000s    20
## 238                                 San Marino   2000s     1
## 239                      Sao Tome and Principe   2000s     5
## 240                               Saudi Arabia   2000s  1589
## 241                                     Serbia   2000s   677
## 242                                 Seychelles   2000s   120
## 243                               Sierra Leone   2000s  2438
## 244                                   Slovenia   2000s   205
## 245                                   Suriname   2000s   171
## 246                                  Swaziland   2000s    48
## 247                                 Tajikistan   2000s   295
## 248                                       Togo   2000s  1817
## 249                                      Tonga   2000s    26
## 250                               Turkmenistan   2000s   202
## 251                                     Tuvalu   2000s     3
## 252                                     Uganda   2000s  1524
## 253                       United Arab Emirates   2000s   358
## 254                                 Uzbekistan   2000s  2056
## 255                                    Vanuatu   2000s     6
## 256                             Western Sahara   2000s     1
## 257                                      Yemen   2000s  1395
## 258                                     Zambia   2000s   632

Plot the boxplot graph

Q5plot_box <- ggplot(total_decade, aes(x = Decades, y = value, fill = Decades)) +
  geom_boxplot(aes(x=country, y=value)) +
  coord_cartesian(ylim = c(0,1500)) +
  ggtitle("Number of Immigrants for each decade") +
  theme(plot.title = element_text(hjust = 0.5)) +
  xlab("decades") + ylab("Number of Immigrants") 
Q5plot_box