Image by Gordon Johnson from Pixabay

Image by Gordon Johnson from Pixabay

Data Import

  1. Download flag.csv and flag.names to your working directory. Make sure to set your working directory appropriately!

  2. Let’s look at some information about this file. Open flag.names in RStudio by double clicking it in the files pane in bottom left. Read through this file.

  1. Import the fla(g.csv data into R. Store it in a data.frame named flag_df.
flag_df<-read.csv("flag.csv")
  1. Check to make sure the class of flag_df is data.frame. Then find the )dimensions of flag_df.
data.frame(flag_df)
##       X                     name landmass zone  area population language
## 1     1              Afghanistan        5    1   648         16       10
## 2     2                  Albania        3    1    29          3        6
## 3     3                  Algeria        4    1  2388         20        8
## 4     4           American-Samoa        6    3     0          0        1
## 5     5                  Andorra        3    1     0          0        6
## 6     6                   Angola        4    2  1247          7       10
## 7     7                 Anguilla        1    4     0          0        1
## 8     8          Antigua-Barbuda        1    4     0          0        1
## 9     9                Argentina        2    3  2777         28        2
## 10   10                Argentine        2    3  2777         28        2
## 11   11                Australia        6    2  7690         15        1
## 12   12                  Austria        3    1    84          8        4
## 13   13                  Bahamas        1    4    19          0        1
## 14   14                  Bahrain        5    1     1          0        8
## 15   15               Bangladesh        5    1   143         90        6
## 16   16                 Barbados        1    4     0          0        1
## 17   17                  Belgium        3    1    31         10        6
## 18   18                   Belize        1    4    23          0        1
## 19   19                    Benin        4    1   113          3        3
## 20   20                  Bermuda        1    4     0          0        1
## 21   21                   Bhutan        5    1    47          1       10
## 22   22                  Bolivia        2    3  1099          6        2
## 23   23                 Botswana        4    2   600          1       10
## 24   24                   Brazil        2    3  8512        119        6
## 25   25     British-Virgin-Isles        1    4     0          0        1
## 26   26                   Brunei        5    1     6          0       10
## 27   27                 Bulgaria        3    1   111          9        5
## 28   28                  Burkina        4    4   274          7        3
## 29   29                    Burma        5    1   678         35       10
## 30   30                  Burundi        4    2    28          4       10
## 31   31                 Cameroon        4    1   474          8        3
## 32   32                   Canada        1    4  9976         24        1
## 33   33       Cape-Verde-Islands        4    4     4          0        6
## 34   34           Cayman-Islands        1    4     0          0        1
## 35   35 Central-African-Republic        4    1   623          2       10
## 36   36                     Chad        4    1  1284          4        3
## 37   37                    Chile        2    3   757         11        2
## 38   38                    China        5    1  9561       1008        7
## 39   39                 Colombia        2    4  1139         28        2
## 40   40          Comorro-Islands        4    2     2          0        3
## 41   41                    Congo        4    2   342          2       10
## 42   42             Cook-Islands        6    3     0          0        1
## 43   43               Costa-Rica        1    4    51          2        2
## 44   44                     Cuba        1    4   115         10        2
## 45   45                   Cyprus        3    1     9          1        6
## 46   46           Czechoslovakia        3    1   128         15        5
## 47   47                  Denmark        3    1    43          5        6
## 48   48                 Djibouti        4    1    22          0        3
## 49   49                 Dominica        1    4     0          0        1
## 50   50       Dominican-Republic        1    4    49          6        2
## 51   51                  Ecuador        2    3   284          8        2
## 52   52                    Egypt        4    1  1001         47        8
## 53   53              El-Salvador        1    4    21          5        2
## 54   54        Equatorial-Guinea        4    1    28          0       10
## 55   55                 Ethiopia        4    1  1222         31       10
## 56   56                  Faeroes        3    4     1          0        6
## 57   57       Falklands-Malvinas        2    3    12          0        1
## 58   58                     Fiji        6    2    18          1        1
## 59   59                  Finland        3    1   337          5        9
## 60   60                   France        3    1   547         54        3
## 61   61            French-Guiana        2    4    91          0        3
## 62   62         French-Polynesia        6    3     4          0        3
## 63   63                    Gabon        4    2   268          1       10
## 64   64                   Gambia        4    4    10          1        1
## 65   65              Germany-DDR        3    1   108         17        4
## 66   66              Germany-FRG        3    1   249         61        4
## 67   67                    Ghana        4    4   239         14        1
## 68   68                Gibraltar        3    4     0          0        1
## 69   69                   Greece        3    1   132         10        6
## 70   70                Greenland        1    4  2176          0        6
## 71   71                  Grenada        1    4     0          0        1
## 72   72                     Guam        6    1     0          0        1
## 73   73                Guatemala        1    4   109          8        2
## 74   74                   Guinea        4    4   246          6        3
## 75   75            Guinea-Bissau        4    4    36          1        6
## 76   76                   Guyana        2    4   215          1        1
## 77   77                    Haiti        1    4    28          6        3
## 78   78                 Honduras        1    4   112          4        2
## 79   79                Hong-Kong        5    1     1          5        7
## 80   80                  Hungary        3    1    93         11        9
## 81   81                  Iceland        3    4   103          0        6
## 82   82                    India        5    1  3268        684        6
## 83   83                Indonesia        6    2  1904        157       10
## 84   84                     Iran        5    1  1648         39        6
## 85   85                     Iraq        5    1   435         14        8
## 86   86                  Ireland        3    4    70          3        1
## 87   87                   Israel        5    1    21          4       10
## 88   88                    Italy        3    1   301         57        6
## 89   89              Ivory-Coast        4    4   323          7        3
## 90   90                  Jamaica        1    4    11          2        1
## 91   91                    Japan        5    1   372        118        9
## 92   92                   Jordan        5    1    98          2        8
## 93   93                Kampuchea        5    1   181          6       10
## 94   94                    Kenya        4    1   583         17       10
## 95   95                 Kiribati        6    1     0          0        1
## 96   96                   Kuwait        5    1    18          2        8
## 97   97                     Laos        5    1   236          3       10
## 98   98                  Lebanon        5    1    10          3        8
## 99   99                  Lesotho        4    2    30          1       10
## 100 100                  Liberia        4    4   111          1       10
## 101 101                    Libya        4    1  1760          3        8
## 102 102            Liechtenstein        3    1     0          0        4
## 103 103               Luxembourg        3    1     3          0        4
## 104 104                 Malagasy        4    2   587          9       10
## 105 105                   Malawi        4    2   118          6       10
## 106 106                 Malaysia        5    1   333         13       10
## 107 107          Maldive-Islands        5    1     0          0       10
## 108 108                     Mali        4    4  1240          7        3
## 109 109                    Malta        3    1     0          0       10
## 110 110                 Marianas        6    1     0          0       10
## 111 111               Mauritania        4    4  1031          2        8
## 112 112                Mauritius        4    2     2          1        1
## 113 113                   Mexico        1    4  1973         77        2
## 114 114               Micronesia        6    1     1          0       10
## 115 115                   Monaco        3    1     0          0        3
## 116 116                 Mongolia        5    1  1566          2       10
## 117 117               Montserrat        1    4     0          0        1
## 118 118                  Morocco        4    4   447         20        8
## 119 119               Mozambique        4    2   783         12       10
## 120 120                    Nauru        6    2     0          0       10
## 121 121                    Nepal        5    1   140         16       10
## 122 122              Netherlands        3    1    41         14        6
## 123 123     Netherlands-Antilles        1    4     0          0        6
## 124 124              New-Zealand        6    2   268          2        1
## 125 125                Nicaragua        1    4   128          3        2
## 126 126                    Niger        4    1  1267          5        3
## 127 127                  Nigeria        4    1   925         56       10
## 128 128                     Niue        6    3     0          0        1
## 129 129              North-Korea        5    1   121         18       10
## 130 130              North-Yemen        5    1   195          9        8
## 131 131                   Norway        3    1   324          4        6
## 132 132                     Oman        5    1   212          1        8
## 133 133                 Pakistan        5    1   804         84        6
## 134 134                   Panama        2    4    76          2        2
## 135 135         Papua-New-Guinea        6    2   463          3        1
## 136 136                  Parguay        2    3   407          3        2
## 137 137                     Peru        2    3  1285         14        2
## 138 138              Philippines        6    1   300         48       10
## 139 139                   Poland        3    1   313         36        5
## 140 140                 Portugal        3    4    92         10        6
## 141 141              Puerto-Rico        1    4     9          3        2
## 142 142                    Qatar        5    1    11          0        8
## 143 143                  Romania        3    1   237         22        6
## 144 144                   Rwanda        4    2    26          5       10
## 145 145               San-Marino        3    1     0          0        6
## 146 146                 Sao-Tome        4    1     0          0        6
## 147 147             Saudi-Arabia        5    1  2150          9        8
## 148 148                  Senegal        4    4   196          6        3
## 149 149               Seychelles        4    2     0          0        1
## 150 150             Sierra-Leone        4    4    72          3        1
## 151 151                Singapore        5    1     1          3        7
## 152 152          Soloman-Islands        6    2    30          0        1
## 153 153                  Somalia        4    1   637          5       10
## 154 154             South-Africa        4    2  1221         29        6
## 155 155              South-Korea        5    1    99         39       10
## 156 156              South-Yemen        5    1   288          2        8
## 157 157                    Spain        3    4   505         38        2
## 158 158                Sri-Lanka        5    1    66         15       10
## 159 159                St-Helena        4    3     0          0        1
## 160 160           St-Kitts-Nevis        1    4     0          0        1
## 161 161                 St-Lucia        1    4     0          0        1
## 162 162               St-Vincent        1    4     0          0        1
## 163 163                    Sudan        4    1  2506         20        8
## 164 164                  Surinam        2    4    63          0        6
## 165 165                Swaziland        4    2    17          1       10
## 166 166                   Sweden        3    1   450          8        6
## 167 167              Switzerland        3    1    41          6        4
## 168 168                    Syria        5    1   185         10        8
## 169 169                   Taiwan        5    1    36         18        7
## 170 170                 Tanzania        4    2   945         18       10
## 171 171                 Thailand        5    1   514         49       10
## 172 172                     Togo        4    1    57          2        3
## 173 173                    Tonga        6    2     1          0       10
## 174 174          Trinidad-Tobago        2    4     5          1        1
## 175 175                  Tunisia        4    1   164          7        8
## 176 176                   Turkey        5    1   781         45        9
## 177 177      Turks-Cocos-Islands        1    4     0          0        1
## 178 178                   Tuvalu        6    2     0          0        1
## 179 179                      UAE        5    1    84          1        8
## 180 180                   Uganda        4    1   236         13       10
## 181 181                       UK        3    4   245         56        1
## 182 182                  Uruguay        2    3   178          3        2
## 183 183          US-Virgin-Isles        1    4     0          0        1
## 184 184                      USA        1    4  9363        231        1
## 185 185                     USSR        5    1 22402        274        5
## 186 186                  Vanuatu        6    2    15          0        6
## 187 187             Vatican-City        3    1     0          0        6
## 188 188                Venezuela        2    4   912         15        2
## 189 189                  Vietnam        5    1   333         60       10
## 190 190            Western-Samoa        6    3     3          0        1
## 191 191               Yugoslavia        3    1   256         22        6
## 192 192                    Zaire        4    2   905         28       10
## 193 193                   Zambia        4    2   753          6       10
## 194 194                 Zimbabwe        4    2   391          8       10
##     religion bars stripes colours red green blue gold white black orange
## 1          2    0       3       5   1     1    0    1     1     1      0
## 2          6    0       0       3   1     0    0    1     0     1      0
## 3          2    2       0       3   1     1    0    0     1     0      0
## 4          1    0       0       5   1     0    1    1     1     0      1
## 5          0    3       0       3   1     0    1    1     0     0      0
## 6          5    0       2       3   1     0    0    1     0     1      0
## 7          1    0       1       3   0     0    1    0     1     0      1
## 8          1    0       1       5   1     0    1    1     1     1      0
## 9          0    0       3       2   0     0    1    0     1     0      0
## 10         0    0       3       3   0     0    1    1     1     0      0
## 11         1    0       0       3   1     0    1    0     1     0      0
## 12         0    0       3       2   1     0    0    0     1     0      0
## 13         1    0       3       3   0     0    1    1     0     1      0
## 14         2    0       0       2   1     0    0    0     1     0      0
## 15         2    0       0       2   1     1    0    0     0     0      0
## 16         1    3       0       3   0     0    1    1     0     1      0
## 17         0    3       0       3   1     0    0    1     0     1      0
## 18         1    0       2       8   1     1    1    1     1     1      1
## 19         5    0       0       2   1     1    0    0     0     0      0
## 20         1    0       0       6   1     1    1    1     1     1      0
## 21         3    0       0       4   1     0    0    0     1     1      1
## 22         0    0       3       3   1     1    0    1     0     0      0
## 23         5    0       5       3   0     0    1    0     1     1      0
## 24         0    0       0       4   0     1    1    1     1     0      0
## 25         1    0       0       6   1     1    1    1     1     0      1
## 26         2    0       0       4   1     0    0    1     1     1      0
## 27         6    0       3       5   1     1    1    1     1     0      0
## 28         5    0       2       3   1     1    0    1     0     0      0
## 29         3    0       0       3   1     0    1    0     1     0      0
## 30         5    0       0       3   1     1    0    0     1     0      0
## 31         1    3       0       3   1     1    0    1     0     0      0
## 32         1    2       0       2   1     0    0    0     1     0      0
## 33         0    1       2       5   1     1    0    1     0     1      1
## 34         1    0       0       6   1     1    1    1     1     0      1
## 35         5    1       0       5   1     1    1    1     1     0      0
## 36         5    3       0       3   1     0    1    1     0     0      0
## 37         0    0       2       3   1     0    1    0     1     0      0
## 38         6    0       0       2   1     0    0    1     0     0      0
## 39         0    0       3       3   1     0    1    1     0     0      0
## 40         2    0       0       2   0     1    0    0     1     0      0
## 41         5    0       0       3   1     1    0    1     0     0      0
## 42         1    0       0       4   1     0    1    0     1     0      0
## 43         0    0       5       3   1     0    1    0     1     0      0
## 44         6    0       5       3   1     0    1    0     1     0      0
## 45         1    0       0       3   0     1    0    1     1     0      0
## 46         6    0       0       3   1     0    1    0     1     0      0
## 47         1    0       0       2   1     0    0    0     1     0      0
## 48         2    0       0       4   1     1    1    0     1     0      0
## 49         1    0       0       6   1     1    1    1     1     1      0
## 50         0    0       0       3   1     0    1    0     1     0      0
## 51         0    0       3       3   1     0    1    1     0     0      0
## 52         2    0       3       4   1     0    0    1     1     1      0
## 53         0    0       3       2   0     0    1    0     1     0      0
## 54         5    0       3       4   1     1    1    0     1     0      0
## 55         1    0       3       3   1     1    0    1     0     0      0
## 56         1    0       0       3   1     0    1    0     1     0      0
## 57         1    0       0       6   1     1    1    1     1     0      0
## 58         1    0       0       7   1     1    1    1     1     0      1
## 59         1    0       0       2   0     0    1    0     1     0      0
## 60         0    3       0       3   1     0    1    0     1     0      0
## 61         0    3       0       3   1     0    1    0     1     0      0
## 62         0    0       3       5   1     0    1    1     1     1      0
## 63         5    0       3       3   0     1    1    1     0     0      0
## 64         5    0       5       4   1     1    1    0     1     0      0
## 65         6    0       3       3   1     0    0    1     0     1      0
## 66         1    0       3       3   1     0    0    1     0     1      0
## 67         5    0       3       4   1     1    0    1     0     1      0
## 68         1    0       1       3   1     0    0    1     1     0      0
## 69         1    0       9       2   0     0    1    0     1     0      0
## 70         1    0       0       2   1     0    0    0     1     0      0
## 71         1    0       0       3   1     1    0    1     0     0      0
## 72         1    0       0       7   1     1    1    1     1     0      1
## 73         0    3       0       2   0     0    1    0     1     0      0
## 74         2    3       0       3   1     1    0    1     0     0      0
## 75         5    1       2       4   1     1    0    1     0     1      0
## 76         4    0       0       5   1     1    0    1     1     1      0
## 77         0    2       0       2   1     0    0    0     0     1      0
## 78         0    0       3       2   0     0    1    0     1     0      0
## 79         3    0       0       6   1     1    1    1     1     0      1
## 80         6    0       3       3   1     1    0    0     1     0      0
## 81         1    0       0       3   1     0    1    0     1     0      0
## 82         4    0       3       4   0     1    1    0     1     0      1
## 83         2    0       2       2   1     0    0    0     1     0      0
## 84         2    0       3       3   1     1    0    0     1     0      0
## 85         2    0       3       4   1     1    0    0     1     1      0
## 86         0    3       0       3   0     1    0    0     1     0      1
## 87         7    0       2       2   0     0    1    0     1     0      0
## 88         0    3       0       3   1     1    0    0     1     0      0
## 89         5    3       0       3   1     1    0    0     1     0      0
## 90         1    0       0       3   0     1    0    1     0     1      0
## 91         7    0       0       2   1     0    0    0     1     0      0
## 92         2    0       3       4   1     1    0    0     1     1      0
## 93         3    0       0       2   1     0    0    1     0     0      0
## 94         5    0       5       4   1     1    0    0     1     1      0
## 95         1    0       0       4   1     0    1    1     1     0      0
## 96         2    0       3       4   1     1    0    0     1     1      0
## 97         6    0       3       3   1     0    1    0     1     0      0
## 98         2    0       2       4   1     1    0    0     1     0      1
## 99         5    2       0       4   1     1    1    0     1     0      0
## 100        5    0      11       3   1     0    1    0     1     0      0
## 101        2    0       0       1   0     1    0    0     0     0      0
## 102        0    0       2       3   1     0    1    1     0     0      0
## 103        0    0       3       3   1     0    1    0     1     0      0
## 104        1    1       2       3   1     1    0    0     1     0      0
## 105        5    0       3       3   1     1    0    0     0     1      0
## 106        2    0      14       4   1     0    1    1     1     0      0
## 107        2    0       0       3   1     1    0    0     1     0      0
## 108        2    3       0       3   1     1    0    1     0     0      0
## 109        0    2       0       3   1     0    0    0     1     1      0
## 110        1    0       0       3   0     0    1    0     1     0      0
## 111        2    0       0       2   0     1    0    1     0     0      0
## 112        4    0       4       4   1     1    1    1     0     0      0
## 113        0    3       0       4   1     1    0    0     1     0      1
## 114        1    0       0       2   0     0    1    0     1     0      0
## 115        0    0       2       2   1     0    0    0     1     0      0
## 116        6    3       0       3   1     0    1    1     0     0      0
## 117        1    0       0       7   1     1    1    1     1     1      0
## 118        2    0       0       2   1     1    0    0     0     0      0
## 119        5    0       5       5   1     1    0    1     1     1      0
## 120        1    0       3       3   0     0    1    1     1     0      0
## 121        4    0       0       3   0     0    1    0     1     0      1
## 122        1    0       3       3   1     0    1    0     1     0      0
## 123        1    0       1       3   1     0    1    0     1     0      0
## 124        1    0       0       3   1     0    1    0     1     0      0
## 125        0    0       3       2   0     0    1    0     1     0      0
## 126        2    0       3       3   0     1    0    0     1     0      1
## 127        2    3       0       2   0     1    0    0     1     0      0
## 128        1    0       0       4   1     0    1    1     1     0      0
## 129        6    0       5       3   1     0    1    0     1     0      0
## 130        2    0       3       4   1     1    0    0     1     1      0
## 131        1    0       0       3   1     0    1    0     1     0      0
## 132        2    0       2       3   1     1    0    0     1     0      0
## 133        2    1       0       2   0     1    0    0     1     0      0
## 134        0    0       0       3   1     0    1    0     1     0      0
## 135        5    0       0       4   1     0    0    1     1     1      0
## 136        0    0       3       6   1     1    1    1     1     1      0
## 137        0    3       0       2   1     0    0    0     1     0      0
## 138        0    0       0       4   1     0    1    1     1     0      0
## 139        6    0       2       2   1     0    0    0     1     0      0
## 140        0    0       0       5   1     1    1    1     1     0      0
## 141        0    0       5       3   1     0    1    0     1     0      0
## 142        2    0       0       2   0     0    0    0     1     0      1
## 143        6    3       0       7   1     1    1    1     1     0      1
## 144        5    3       0       4   1     1    0    1     0     1      0
## 145        0    0       2       2   0     0    1    0     1     0      0
## 146        0    0       3       4   1     1    0    1     0     1      0
## 147        2    0       0       2   0     1    0    0     1     0      0
## 148        2    3       0       3   1     1    0    1     0     0      0
## 149        1    0       0       3   1     1    0    0     1     0      0
## 150        5    0       3       3   0     1    1    0     1     0      0
## 151        3    0       2       2   1     0    0    0     1     0      0
## 152        1    0       0       4   0     1    1    1     1     0      0
## 153        2    0       0       2   0     0    1    0     1     0      0
## 154        1    0       3       5   1     1    1    0     1     0      1
## 155        7    0       0       4   1     0    1    0     1     1      0
## 156        2    0       3       4   1     0    1    0     1     1      0
## 157        0    0       3       2   1     0    0    1     0     0      0
## 158        3    2       0       4   0     1    0    1     0     0      1
## 159        1    0       0       7   1     1    1    1     1     0      1
## 160        1    0       0       5   1     1    0    1     1     1      0
## 161        1    0       0       4   0     0    1    1     1     1      0
## 162        1    5       0       4   0     1    1    1     1     0      0
## 163        2    0       3       4   1     1    0    0     1     1      0
## 164        1    0       5       4   1     1    0    1     1     0      0
## 165        1    0       5       7   1     0    1    1     1     1      1
## 166        1    0       0       2   0     0    1    1     0     0      0
## 167        1    0       0       2   1     0    0    0     1     0      0
## 168        2    0       3       4   1     1    0    0     1     1      0
## 169        3    0       0       3   1     0    1    0     1     0      0
## 170        5    0       0       4   0     1    1    1     0     1      0
## 171        3    0       5       3   1     0    1    0     1     0      0
## 172        7    0       5       4   1     1    0    1     1     0      0
## 173        1    0       0       2   1     0    0    0     1     0      0
## 174        1    0       0       3   1     0    0    0     1     1      0
## 175        2    0       0       2   1     0    0    0     1     0      0
## 176        2    0       0       2   1     0    0    0     1     0      0
## 177        1    0       0       6   1     1    1    1     1     0      1
## 178        1    0       0       5   1     0    1    1     1     0      0
## 179        2    1       3       4   1     1    0    0     1     1      0
## 180        5    0       6       5   1     0    0    1     1     1      0
## 181        1    0       0       3   1     0    1    0     1     0      0
## 182        0    0       9       3   0     0    1    1     1     0      0
## 183        1    0       0       6   1     1    1    1     1     0      0
## 184        1    0      13       3   1     0    1    0     1     0      0
## 185        6    0       0       2   1     0    0    1     0     0      0
## 186        1    0       0       4   1     1    0    1     0     1      0
## 187        0    2       0       4   1     0    0    1     1     1      0
## 188        0    0       3       7   1     1    1    1     1     1      1
## 189        6    0       0       2   1     0    0    1     0     0      0
## 190        1    0       0       3   1     0    1    0     1     0      0
## 191        6    0       3       4   1     0    1    1     1     0      0
## 192        5    0       0       4   1     1    0    1     0     0      1
## 193        5    3       0       4   1     1    0    0     0     1      1
## 194        5    0       7       5   1     1    0    1     1     1      0
##     mainhue circles crosses saltires quarters sunstars crescent triangle
## 1     green       0       0        0        0        1        0        0
## 2       red       0       0        0        0        1        0        0
## 3     green       0       0        0        0        1        1        0
## 4      blue       0       0        0        0        0        0        1
## 5      gold       0       0        0        0        0        0        0
## 6       red       0       0        0        0        1        0        0
## 7     white       0       0        0        0        0        0        0
## 8       red       0       0        0        0        1        0        1
## 9      blue       0       0        0        0        0        0        0
## 10     blue       0       0        0        0        1        0        0
## 11     blue       0       1        1        1        6        0        0
## 12      red       0       0        0        0        0        0        0
## 13     blue       0       0        0        0        0        0        1
## 14      red       0       0        0        0        0        0        0
## 15    green       1       0        0        0        0        0        0
## 16     blue       0       0        0        0        0        0        0
## 17     gold       0       0        0        0        0        0        0
## 18     blue       1       0        0        0        0        0        0
## 19    green       0       0        0        0        1        0        0
## 20      red       1       1        1        1        0        0        0
## 21   orange       4       0        0        0        0        0        0
## 22      red       0       0        0        0        0        0        0
## 23     blue       0       0        0        0        0        0        0
## 24    green       1       0        0        0       22        0        0
## 25     blue       0       1        1        1        0        0        0
## 26     gold       0       0        0        0        0        0        1
## 27      red       0       0        0        0        1        0        0
## 28      red       0       0        0        0        1        0        0
## 29      red       0       0        0        1       14        0        0
## 30      red       1       0        1        0        3        0        0
## 31     gold       0       0        0        0        1        0        0
## 32      red       0       0        0        0        0        0        0
## 33     gold       0       0        0        0        1        0        0
## 34     blue       1       1        1        1        4        0        0
## 35     gold       0       0        0        0        1        0        0
## 36     gold       0       0        0        0        0        0        0
## 37      red       0       0        0        1        1        0        0
## 38      red       0       0        0        0        5        0        0
## 39     gold       0       0        0        0        0        0        0
## 40    green       0       0        0        0        4        1        0
## 41      red       0       0        0        0        1        0        0
## 42     blue       1       1        1        1       15        0        0
## 43     blue       0       0        0        0        0        0        0
## 44     blue       0       0        0        0        1        0        1
## 45    white       0       0        0        0        0        0        0
## 46    white       0       0        0        0        0        0        1
## 47      red       0       1        0        0        0        0        0
## 48     blue       0       0        0        0        1        0        1
## 49    green       1       0        0        0       10        0        0
## 50     blue       0       1        0        0        0        0        0
## 51     gold       0       0        0        0        0        0        0
## 52    black       0       0        0        0        0        0        0
## 53     blue       0       0        0        0        0        0        0
## 54    green       0       0        0        0        0        0        1
## 55    green       0       0        0        0        0        0        0
## 56    white       0       1        0        0        0        0        0
## 57     blue       1       1        1        1        0        0        0
## 58     blue       0       2        1        1        0        0        0
## 59    white       0       1        0        0        0        0        0
## 60    white       0       0        0        0        0        0        0
## 61    white       0       0        0        0        0        0        0
## 62      red       1       0        0        0        1        0        0
## 63    green       0       0        0        0        0        0        0
## 64      red       0       0        0        0        0        0        0
## 65     gold       0       0        0        0        0        0        0
## 66    black       0       0        0        0        0        0        0
## 67      red       0       0        0        0        1        0        0
## 68    white       0       0        0        0        0        0        0
## 69     blue       0       1        0        1        0        0        0
## 70    white       1       0        0        0        0        0        0
## 71     gold       1       0        0        0        7        0        1
## 72     blue       0       0        0        0        0        0        0
## 73     blue       0       0        0        0        0        0        0
## 74     gold       0       0        0        0        0        0        0
## 75     gold       0       0        0        0        1        0        0
## 76    green       0       0        0        0        0        0        1
## 77    black       0       0        0        0        0        0        0
## 78     blue       0       0        0        0        5        0        0
## 79     blue       1       1        1        1        0        0        0
## 80      red       0       0        0        0        0        0        0
## 81     blue       0       1        0        0        0        0        0
## 82   orange       1       0        0        0        0        0        0
## 83      red       0       0        0        0        0        0        0
## 84      red       0       0        0        0        0        0        0
## 85      red       0       0        0        0        3        0        0
## 86    white       0       0        0        0        0        0        0
## 87    white       0       0        0        0        1        0        0
## 88    white       0       0        0        0        0        0        0
## 89    white       0       0        0        0        0        0        0
## 90    green       0       0        1        0        0        0        1
## 91    white       1       0        0        0        1        0        0
## 92    black       0       0        0        0        1        0        1
## 93      red       0       0        0        0        0        0        0
## 94      red       1       0        0        0        0        0        0
## 95      red       0       0        0        0        1        0        0
## 96    green       0       0        0        0        0        0        0
## 97      red       1       0        0        0        0        0        0
## 98      red       0       0        0        0        0        0        0
## 99     blue       0       0        0        0        0        0        0
## 100     red       0       0        0        1        1        0        0
## 101   green       0       0        0        0        0        0        0
## 102     red       0       0        0        0        0        0        0
## 103     red       0       0        0        0        0        0        0
## 104     red       0       0        0        0        0        0        0
## 105     red       0       0        0        0        1        0        0
## 106     red       0       0        0        1        1        1        0
## 107     red       0       0        0        0        0        1        0
## 108    gold       0       0        0        0        0        0        0
## 109     red       0       1        0        0        0        0        0
## 110    blue       0       0        0        0        1        0        0
## 111   green       0       0        0        0        1        1        0
## 112     red       0       0        0        0        0        0        0
## 113   green       0       0        0        0        0        0        0
## 114    blue       0       0        0        0        4        0        0
## 115     red       0       0        0        0        0        0        0
## 116     red       2       0        0        0        1        1        1
## 117    blue       0       2        1        1        0        0        0
## 118     red       0       0        0        0        1        0        0
## 119    gold       0       0        0        0        1        0        1
## 120    blue       0       0        0        0        1        0        0
## 121   brown       0       0        0        0        2        1        0
## 122     red       0       0        0        0        0        0        0
## 123   white       0       0        0        0        6        0        0
## 124    blue       0       1        1        1        4        0        0
## 125    blue       0       0        0        0        0        0        0
## 126  orange       1       0        0        0        0        0        0
## 127   green       0       0        0        0        0        0        0
## 128    gold       1       1        1        1        5        0        0
## 129    blue       1       0        0        0        1        0        0
## 130     red       0       0        0        0        1        0        0
## 131     red       0       1        0        0        0        0        0
## 132     red       0       0        0        0        0        0        0
## 133   green       0       0        0        0        1        1        0
## 134     red       0       0        0        4        2        0        0
## 135   black       0       0        0        0        5        0        1
## 136     red       1       0        0        0        1        0        0
## 137     red       0       0        0        0        0        0        0
## 138    blue       0       0        0        0        4        0        1
## 139   white       0       0        0        0        0        0        0
## 140     red       1       0        0        0        0        0        0
## 141     red       0       0        0        0        1        0        1
## 142   brown       0       0        0        0        0        0        0
## 143     red       0       0        0        0        2        0        0
## 144     red       0       0        0        0        0        0        0
## 145   white       0       0        0        0        0        0        0
## 146   green       0       0        0        0        2        0        1
## 147   green       0       0        0        0        0        0        0
## 148   green       0       0        0        0        1        0        0
## 149     red       0       0        0        0        0        0        0
## 150   green       0       0        0        0        0        0        0
## 151   white       0       0        0        0        5        1        0
## 152   green       0       0        0        0        5        0        1
## 153    blue       0       0        0        0        1        0        0
## 154  orange       0       1        1        0        0        0        0
## 155   white       1       0        0        0        0        0        0
## 156     red       0       0        0        0        1        0        1
## 157     red       0       0        0        0        0        0        0
## 158    gold       0       0        0        0        0        0        0
## 159    blue       0       1        1        1        0        0        0
## 160   green       0       0        0        0        2        0        1
## 161    blue       0       0        0        0        0        0        1
## 162   green       0       0        0        0        0        0        0
## 163     red       0       0        0        0        0        0        1
## 164     red       0       0        0        0        1        0        0
## 165    blue       0       0        0        0        0        0        0
## 166    blue       0       1        0        0        0        0        0
## 167     red       0       1        0        0        0        0        0
## 168     red       0       0        0        0        2        0        0
## 169     red       1       0        0        1        1        0        0
## 170   green       0       0        0        0        0        0        1
## 171     red       0       0        0        0        0        0        0
## 172   green       0       0        0        1        1        0        0
## 173     red       0       1        0        1        0        0        0
## 174     red       0       0        0        0        0        0        1
## 175     red       1       0        0        0        1        1        0
## 176     red       0       0        0        0        1        1        0
## 177    blue       0       1        1        1        0        0        0
## 178    blue       0       1        1        1        9        0        0
## 179   green       0       0        0        0        0        0        0
## 180    gold       1       0        0        0        0        0        0
## 181     red       0       1        1        0        0        0        0
## 182   white       0       0        0        1        1        0        0
## 183   white       0       0        0        0        0        0        0
## 184   white       0       0        0        1       50        0        0
## 185     red       0       0        0        0        1        0        0
## 186     red       0       0        0        0        0        0        1
## 187    gold       0       0        0        0        0        0        0
## 188     red       0       0        0        0        7        0        0
## 189     red       0       0        0        0        1        0        0
## 190     red       0       0        0        1        5        0        0
## 191     red       0       0        0        0        1        0        0
## 192   green       1       0        0        0        0        0        0
## 193   green       0       0        0        0        0        0        0
## 194   green       0       0        0        0        1        0        1
##     icon animate text topleft botright
## 1      1       0    0   black    green
## 2      0       1    0     red      red
## 3      0       0    0   green    white
## 4      1       1    0    blue      red
## 5      0       0    0    blue      red
## 6      1       0    0     red    black
## 7      0       1    0   white     blue
## 8      0       0    0   black      red
## 9      0       0    0    blue     blue
## 10     0       0    0    blue     blue
## 11     0       0    0   white     blue
## 12     0       0    0     red      red
## 13     0       0    0    blue     blue
## 14     0       0    0   white      red
## 15     0       0    0   green    green
## 16     1       0    0    blue     blue
## 17     0       0    0   black      red
## 18     1       1    1     red      red
## 19     0       0    0   green    green
## 20     1       1    0   white      red
## 21     0       1    0  orange      red
## 22     0       0    0     red    green
## 23     0       0    0    blue     blue
## 24     0       0    1   green    green
## 25     1       1    1   white     blue
## 26     1       1    1   white     gold
## 27     1       1    0   white      red
## 28     0       0    0     red    green
## 29     1       1    0    blue      red
## 30     0       0    0   white    white
## 31     0       0    0   green     gold
## 32     0       1    0     red      red
## 33     0       1    0     red    green
## 34     1       1    1   white     blue
## 35     0       0    0    blue     gold
## 36     0       0    0    blue      red
## 37     0       0    0    blue      red
## 38     0       0    0     red      red
## 39     0       0    0    gold      red
## 40     0       0    0   green    green
## 41     1       1    0     red      red
## 42     0       0    0   white     blue
## 43     0       0    0    blue     blue
## 44     0       0    0    blue     blue
## 45     1       1    0   white    white
## 46     0       0    0   white      red
## 47     0       0    0     red      red
## 48     0       0    0   white    green
## 49     0       1    0   green    green
## 50     0       0    0    blue     blue
## 51     0       0    0    gold      red
## 52     0       1    1     red    black
## 53     0       0    0    blue     blue
## 54     0       0    0   green      red
## 55     0       0    0   green      red
## 56     0       0    0   white    white
## 57     1       1    1   white     blue
## 58     1       1    0   white     blue
## 59     0       0    0   white    white
## 60     0       0    0    blue      red
## 61     0       0    0    blue      red
## 62     1       0    0     red      red
## 63     0       0    0   green     blue
## 64     0       0    0     red    green
## 65     1       0    0   black     gold
## 66     0       0    0   black     gold
## 67     0       0    0     red    green
## 68     1       0    0   white      red
## 69     0       0    0    blue     blue
## 70     0       0    0   white      red
## 71     0       1    0     red      red
## 72     1       1    1     red      red
## 73     0       0    0    blue     blue
## 74     0       0    0     red    green
## 75     0       0    0     red    green
## 76     0       0    0   black    green
## 77     0       0    0   black      red
## 78     0       0    0    blue     blue
## 79     1       1    1   white     blue
## 80     0       0    0     red    green
## 81     0       0    0    blue     blue
## 82     1       0    0  orange    green
## 83     0       0    0     red    white
## 84     1       0    1   green      red
## 85     0       0    0     red    black
## 86     0       0    0   green   orange
## 87     0       0    0    blue     blue
## 88     0       0    0   green      red
## 89     0       0    0     red    green
## 90     0       0    0    gold     gold
## 91     0       0    0   white    white
## 92     0       0    0   black    green
## 93     1       0    0     red      red
## 94     1       0    0   black    green
## 95     1       1    0     red     blue
## 96     0       0    0   green      red
## 97     0       0    0     red      red
## 98     0       1    0     red      red
## 99     1       0    0   green     blue
## 100    0       0    0    blue      red
## 101    0       0    0   green    green
## 102    1       0    0    blue      red
## 103    0       0    0     red     blue
## 104    0       0    0   white    green
## 105    0       0    0   black    green
## 106    0       0    0    blue    white
## 107    0       0    0     red      red
## 108    0       0    0   green      red
## 109    1       0    0   white      red
## 110    1       0    0    blue     blue
## 111    0       0    0   green    green
## 112    0       0    0     red    green
## 113    0       1    0   green      red
## 114    0       0    0    blue     blue
## 115    0       0    0     red    white
## 116    1       0    0     red      red
## 117    1       1    0   white     blue
## 118    0       0    0     red      red
## 119    1       0    0   green     gold
## 120    0       0    0    blue     blue
## 121    0       0    0    blue     blue
## 122    0       0    0     red     blue
## 123    0       0    0   white    white
## 124    0       0    0   white     blue
## 125    0       0    0    blue     blue
## 126    0       0    0  orange    green
## 127    0       0    0   green    green
## 128    0       0    0   white     gold
## 129    0       0    0    blue     blue
## 130    0       0    0     red    black
## 131    0       0    0     red      red
## 132    1       0    0     red    green
## 133    0       0    0   white    green
## 134    0       0    0   white    white
## 135    0       1    0     red    black
## 136    1       1    1     red     blue
## 137    0       0    0     red      red
## 138    0       0    0    blue      red
## 139    0       0    0   white      red
## 140    1       0    0   green      red
## 141    0       0    0     red      red
## 142    0       0    0   white    brown
## 143    1       1    1    blue      red
## 144    0       0    1     red    green
## 145    0       0    0   white     blue
## 146    0       0    0   green    green
## 147    1       0    1   green    green
## 148    0       0    0   green      red
## 149    0       0    0     red    green
## 150    0       0    0   green     blue
## 151    0       0    0     red    white
## 152    0       0    0    blue    green
## 153    0       0    0    blue     blue
## 154    0       0    0  orange     blue
## 155    1       0    0   white    white
## 156    0       0    0     red    black
## 157    0       0    0     red      red
## 158    1       1    0    gold     gold
## 159    1       0    0   white     blue
## 160    0       0    0   green      red
## 161    0       0    0    blue     blue
## 162    1       1    1    blue    green
## 163    0       0    0     red    black
## 164    0       0    0   green    green
## 165    1       0    0    blue     blue
## 166    0       0    0    blue     blue
## 167    0       0    0     red      red
## 168    0       0    0     red    black
## 169    0       0    0    blue      red
## 170    0       0    0   green     blue
## 171    0       0    0     red      red
## 172    0       0    0     red    green
## 173    0       0    0   white      red
## 174    0       0    0   white    white
## 175    0       0    0     red      red
## 176    0       0    0     red      red
## 177    1       1    0   white     blue
## 178    0       0    0   white     blue
## 179    0       0    0     red    black
## 180    0       1    0   black      red
## 181    0       0    0   white      red
## 182    0       0    0   white    white
## 183    1       1    1   white    white
## 184    0       0    0    blue      red
## 185    1       0    0     red      red
## 186    0       1    0   black    green
## 187    1       0    0    gold    white
## 188    1       1    0    gold      red
## 189    0       0    0     red      red
## 190    0       0    0    blue      red
## 191    0       0    0    blue      red
## 192    1       1    0   green    green
## 193    0       1    0   green    brown
## 194    1       1    0   green    green
dim(flag_df)
## [1] 194  31
  1. Print out the first 5 lines and the last 5 lines of flag_df.
head(flag_df,5)
##   X           name landmass zone area population language religion bars
## 1 1    Afghanistan        5    1  648         16       10        2    0
## 2 2        Albania        3    1   29          3        6        6    0
## 3 3        Algeria        4    1 2388         20        8        2    2
## 4 4 American-Samoa        6    3    0          0        1        1    0
## 5 5        Andorra        3    1    0          0        6        0    3
##   stripes colours red green blue gold white black orange mainhue circles
## 1       3       5   1     1    0    1     1     1      0   green       0
## 2       0       3   1     0    0    1     0     1      0     red       0
## 3       0       3   1     1    0    0     1     0      0   green       0
## 4       0       5   1     0    1    1     1     0      1    blue       0
## 5       0       3   1     0    1    1     0     0      0    gold       0
##   crosses saltires quarters sunstars crescent triangle icon animate text
## 1       0        0        0        1        0        0    1       0    0
## 2       0        0        0        1        0        0    0       1    0
## 3       0        0        0        1        1        0    0       0    0
## 4       0        0        0        0        0        1    1       1    0
## 5       0        0        0        0        0        0    0       0    0
##   topleft botright
## 1   black    green
## 2     red      red
## 3   green    white
## 4    blue      red
## 5    blue      red
tail(flag_df,5)
##       X          name landmass zone area population language religion bars
## 190 190 Western-Samoa        6    3    3          0        1        1    0
## 191 191    Yugoslavia        3    1  256         22        6        6    0
## 192 192         Zaire        4    2  905         28       10        5    0
## 193 193        Zambia        4    2  753          6       10        5    3
## 194 194      Zimbabwe        4    2  391          8       10        5    0
##     stripes colours red green blue gold white black orange mainhue circles
## 190       0       3   1     0    1    0     1     0      0     red       0
## 191       3       4   1     0    1    1     1     0      0     red       0
## 192       0       4   1     1    0    1     0     0      1   green       1
## 193       0       4   1     1    0    0     0     1      1   green       0
## 194       7       5   1     1    0    1     1     1      0   green       0
##     crosses saltires quarters sunstars crescent triangle icon animate text
## 190       0        0        1        5        0        0    0       0    0
## 191       0        0        0        1        0        0    0       0    0
## 192       0        0        0        0        0        0    1       1    0
## 193       0        0        0        0        0        0    0       1    0
## 194       0        0        0        1        0        1    1       1    0
##     topleft botright
## 190    blue      red
## 191    blue      red
## 192   green    green
## 193   green    brown
## 194   green    green
  1. Print out the summary statistics of each variable of flag_df.
summary(flag_df)
##        X                      name        landmass          zone      
##  Min.   :  1.00   Afghanistan   :  1   Min.   :1.000   Min.   :1.000  
##  1st Qu.: 49.25   Albania       :  1   1st Qu.:3.000   1st Qu.:1.000  
##  Median : 97.50   Algeria       :  1   Median :4.000   Median :2.000  
##  Mean   : 97.50   American-Samoa:  1   Mean   :3.572   Mean   :2.211  
##  3rd Qu.:145.75   Andorra       :  1   3rd Qu.:5.000   3rd Qu.:4.000  
##  Max.   :194.00   Angola        :  1   Max.   :6.000   Max.   :4.000  
##                   (Other)       :188                                  
##       area           population         language        religion    
##  Min.   :    0.0   Min.   :   0.00   Min.   : 1.00   Min.   :0.000  
##  1st Qu.:    9.0   1st Qu.:   0.00   1st Qu.: 2.00   1st Qu.:1.000  
##  Median :  111.0   Median :   4.00   Median : 6.00   Median :1.000  
##  Mean   :  700.0   Mean   :  23.27   Mean   : 5.34   Mean   :2.191  
##  3rd Qu.:  471.2   3rd Qu.:  14.00   3rd Qu.: 9.00   3rd Qu.:4.000  
##  Max.   :22402.0   Max.   :1008.00   Max.   :10.00   Max.   :7.000  
##                                                                     
##       bars           stripes          colours           red        
##  Min.   :0.0000   Min.   : 0.000   Min.   :1.000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.: 0.000   1st Qu.:3.000   1st Qu.:1.0000  
##  Median :0.0000   Median : 0.000   Median :3.000   Median :1.0000  
##  Mean   :0.4536   Mean   : 1.552   Mean   :3.464   Mean   :0.7887  
##  3rd Qu.:0.0000   3rd Qu.: 3.000   3rd Qu.:4.000   3rd Qu.:1.0000  
##  Max.   :5.0000   Max.   :14.000   Max.   :8.000   Max.   :1.0000  
##                                                                    
##      green             blue             gold            white       
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.0000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:1.0000  
##  Median :0.0000   Median :1.0000   Median :0.0000   Median :1.0000  
##  Mean   :0.4691   Mean   :0.5103   Mean   :0.4691   Mean   :0.7526  
##  3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000   3rd Qu.:1.0000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.0000  
##                                                                     
##      black           orange         mainhue      circles      
##  Min.   :0.000   Min.   :0.000   red    :71   Min.   :0.0000  
##  1st Qu.:0.000   1st Qu.:0.000   blue   :40   1st Qu.:0.0000  
##  Median :0.000   Median :0.000   green  :31   Median :0.0000  
##  Mean   :0.268   Mean   :0.134   white  :22   Mean   :0.1701  
##  3rd Qu.:1.000   3rd Qu.:0.000   gold   :19   3rd Qu.:0.0000  
##  Max.   :1.000   Max.   :1.000   black  : 5   Max.   :4.0000  
##                                  (Other): 6                   
##     crosses          saltires          quarters         sunstars     
##  Min.   :0.0000   Min.   :0.00000   Min.   :0.0000   Min.   : 0.000  
##  1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.0000   1st Qu.: 0.000  
##  Median :0.0000   Median :0.00000   Median :0.0000   Median : 0.000  
##  Mean   :0.1495   Mean   :0.09278   Mean   :0.1495   Mean   : 1.387  
##  3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.0000   3rd Qu.: 1.000  
##  Max.   :2.0000   Max.   :1.00000   Max.   :4.0000   Max.   :50.000  
##                                                                      
##     crescent         triangle           icon           animate     
##  Min.   :0.0000   Min.   :0.0000   Min.   :0.0000   Min.   :0.000  
##  1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.000  
##  Median :0.0000   Median :0.0000   Median :0.0000   Median :0.000  
##  Mean   :0.0567   Mean   :0.1392   Mean   :0.2526   Mean   :0.201  
##  3rd Qu.:0.0000   3rd Qu.:0.0000   3rd Qu.:0.7500   3rd Qu.:0.000  
##  Max.   :1.0000   Max.   :1.0000   Max.   :1.0000   Max.   :1.000  
##                                                                    
##       text           topleft      botright 
##  Min.   :0.00000   black :12   red    :69  
##  1st Qu.:0.00000   blue  :43   blue   :47  
##  Median :0.00000   gold  : 6   green  :40  
##  Mean   :0.08247   green :32   white  :17  
##  3rd Qu.:0.00000   orange: 4   black  : 9  
##  Max.   :1.00000   red   :56   gold   : 9  
##                    white :41   (Other): 3
  1. Print out the structure of flag_df.
str(flag_df)
## 'data.frame':    194 obs. of  31 variables:
##  $ X         : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ name      : Factor w/ 194 levels "Afghanistan",..: 1 2 3 4 5 6 7 8 9 10 ...
##  $ landmass  : int  5 3 4 6 3 4 1 1 2 2 ...
##  $ zone      : int  1 1 1 3 1 2 4 4 3 3 ...
##  $ area      : int  648 29 2388 0 0 1247 0 0 2777 2777 ...
##  $ population: int  16 3 20 0 0 7 0 0 28 28 ...
##  $ language  : int  10 6 8 1 6 10 1 1 2 2 ...
##  $ religion  : int  2 6 2 1 0 5 1 1 0 0 ...
##  $ bars      : int  0 0 2 0 3 0 0 0 0 0 ...
##  $ stripes   : int  3 0 0 0 0 2 1 1 3 3 ...
##  $ colours   : int  5 3 3 5 3 3 3 5 2 3 ...
##  $ red       : int  1 1 1 1 1 1 0 1 0 0 ...
##  $ green     : int  1 0 1 0 0 0 0 0 0 0 ...
##  $ blue      : int  0 0 0 1 1 0 1 1 1 1 ...
##  $ gold      : int  1 1 0 1 1 1 0 1 0 1 ...
##  $ white     : int  1 0 1 1 0 0 1 1 1 1 ...
##  $ black     : int  1 1 0 0 0 1 0 1 0 0 ...
##  $ orange    : int  0 0 0 1 0 0 1 0 0 0 ...
##  $ mainhue   : Factor w/ 8 levels "black","blue",..: 5 7 5 2 4 7 8 7 2 2 ...
##  $ circles   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ crosses   : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ saltires  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ quarters  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ sunstars  : int  1 1 1 0 0 1 0 1 0 1 ...
##  $ crescent  : int  0 0 1 0 0 0 0 0 0 0 ...
##  $ triangle  : int  0 0 0 1 0 0 0 1 0 0 ...
##  $ icon      : int  1 0 0 1 0 1 0 0 0 0 ...
##  $ animate   : int  0 1 0 1 0 0 1 0 0 0 ...
##  $ text      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ topleft   : Factor w/ 7 levels "black","blue",..: 1 6 4 2 2 6 7 1 2 2 ...
##  $ botright  : Factor w/ 8 levels "black","blue",..: 5 7 8 7 7 1 2 7 2 2 ...

Data Cleaning/Management

We are going to use the dplyr package.

  1. Load the tidyverse and convert the type of flag_df to tibble.
library(tidyverse)
## -- Attaching packages ------------------------------ tidyverse 1.2.1 --
## v ggplot2 3.2.1     v purrr   0.3.2
## v tibble  2.1.3     v dplyr   0.8.3
## v tidyr   0.8.3     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## -- Conflicts --------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
flag_df<-as_tibble(flag_df)
  1. Find the variable (column) names of flag_df.
glimpse(flag_df)
## Observations: 194
## Variables: 31
## $ X          <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...
## $ name       <fct> Afghanistan, Albania, Algeria, American-Samoa, Ando...
## $ landmass   <int> 5, 3, 4, 6, 3, 4, 1, 1, 2, 2, 6, 3, 1, 5, 5, 1, 3, ...
## $ zone       <int> 1, 1, 1, 3, 1, 2, 4, 4, 3, 3, 2, 1, 4, 1, 1, 4, 1, ...
## $ area       <int> 648, 29, 2388, 0, 0, 1247, 0, 0, 2777, 2777, 7690, ...
## $ population <int> 16, 3, 20, 0, 0, 7, 0, 0, 28, 28, 15, 8, 0, 0, 90, ...
## $ language   <int> 10, 6, 8, 1, 6, 10, 1, 1, 2, 2, 1, 4, 1, 8, 6, 1, 6...
## $ religion   <int> 2, 6, 2, 1, 0, 5, 1, 1, 0, 0, 1, 0, 1, 2, 2, 1, 0, ...
## $ bars       <int> 0, 0, 2, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, ...
## $ stripes    <int> 3, 0, 0, 0, 0, 2, 1, 1, 3, 3, 0, 3, 3, 0, 0, 0, 0, ...
## $ colours    <int> 5, 3, 3, 5, 3, 3, 3, 5, 2, 3, 3, 2, 3, 2, 2, 3, 3, ...
## $ red        <int> 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, ...
## $ green      <int> 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, ...
## $ blue       <int> 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, ...
## $ gold       <int> 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, ...
## $ white      <int> 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, ...
## $ black      <int> 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, ...
## $ orange     <int> 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ mainhue    <fct> green, red, green, blue, gold, red, white, red, blu...
## $ circles    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, ...
## $ crosses    <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...
## $ saltires   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...
## $ quarters   <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, ...
## $ sunstars   <int> 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 6, 0, 0, 0, 0, 0, 0, ...
## $ crescent   <int> 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ triangle   <int> 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, ...
## $ icon       <int> 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, ...
## $ animate    <int> 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ text       <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ topleft    <fct> black, red, green, blue, blue, red, white, black, b...
## $ botright   <fct> green, red, white, red, red, black, blue, red, blue...

Something should look strange about the first column name. Let’s investigate this.

  1. Print out the first column.
flag_df[,1]
## # A tibble: 194 x 1
##        X
##    <int>
##  1     1
##  2     2
##  3     3
##  4     4
##  5     5
##  6     6
##  7     7
##  8     8
##  9     9
## 10    10
## # ... with 184 more rows
  1. Delete the first column of flag_df.
flag_df$X1=NULL
  1. Verify that there are no missing values in flag_df.
sum(is.na(flag_df))
## [1] 0

At this point, we know there are no missing values in the dataset so we will use dplyr to make the dataset a bit more readable to us. Look at the flag.names file again. Under “Attribute Information” look at the variables landmass, zone, language, religion.

Instead of encoding these categories using numbers, we would like to just use the categories in the variables. For example, in the zone column, we want our data to be “NE”, “SE”, “SW”, “NW”, instead of 1, 2, 3, 4.

  1. Change each of the columns landmass, zone, language, and religion to hold their actual categorical data (not their encoded numbers). The type of each of these columns should be Factor.
flag_df$landmass<-as.character(flag_df$landmass)
flag_df$landmass[flag_df$landmass == "1"]<- "NE" 
flag_df$landmass[flag_df$landmass == "2"]<- "SE" 
flag_df$landmass[flag_df$landmass == "3"]<- "SW" 
flag_df$landmass[flag_df$landmass == "4"]<- "NW" 

flag_df$zone<-as.character(flag_df$zone)
flag_df$zone[flag_df$zone == "1"]<- "NE" 
flag_df$zone[flag_df$zone == "2"]<- "SE" 
flag_df$zone[flag_df$zone == "3"]<- "SW" 
flag_df$zone[flag_df$zone == "4"]<- "NW" 

flag_df$language<-as.character(flag_df$language)
flag_df$language[flag_df$language == "1"]<- "NE"
flag_df$language[flag_df$language== "2"]<- "SE" 
flag_df$language[flag_df$language== "3"]<- "SW" 
flag_df$language[flag_df$language== "4"]<- "NW" 


flag_df$religion<-as.character(flag_df$religion)
flag_df$religion[flag_df$religion== "1"]<- "NE" 
flag_df$religion[flag_df$religion== "2"]<- "SE" 
flag_df$religion[flag_df$religion== "3"]<- "SW" 
flag_df$religion[flag_df$religion== "4"]<- "NW" 

flag_df$landmass<-as.factor(flag_df$landmass)
flag_df$zone<-as.factor(flag_df$zone)
flag_df$language<-as.factor(flag_df$language)
flag_df$religion<-as.factor(flag_df$religion)

Notice from our earlier structure command that the data types for columns red, green, blue, gold, white, black, orange, crescent, triangle, icon, animate, text are all integer. Looking at flag.names these integer variables are really just an encoding for true (1) or false (0). We don’t want to compute with these 1s and 0s (for example find a mean). So we should change these to logicals.

  1. Change the column type to logical for the following columns: red, green, blue, gold, white, black, orange, crescent, triangle, icon, animate, and text.
flag_df$red<-as.logical(flag_df$red)
flag_df$green<-as.logical(flag_df$green)
flag_df$blue<-as.logical(flag_df$blue)
flag_df$gold<-as.logical(flag_df$gold)
flag_df$white<-as.logical(flag_df$white)
flag_df$black<-as.logical(flag_df$black)
flag_df$orange<-as.logical(flag_df$orange)
flag_df$crescent<-as.logical(flag_df$crescent)
flag_df$triangle<-as.logical(flag_df$triangle)
flag_df$icon<-as.logical(flag_df$icon)
flag_df$animate<-as.logical(flag_df$animate)
flag_df$text<-as.logical(flag_df$text)

Now that our data is clean, let’s answer some questions about it!

Data Investigation

  1. Print out how many countries have each “mainhue” category.
table(flag_df$mainhue)
## 
##  black   blue  brown   gold  green orange    red  white 
##      5     40      2     19     31      4     71     22
  1. How many countries have the three colors red, white, and blue in their flags? How many countries have ONLY the three colors red, white, and blue in their flags?
library(dplyr)
colors<- flag_df %>%
  filter(red==TRUE&blue==TRUE&white==TRUE&black==FALSE&gold==FALSE&green==FALSE&orange==FALSE)
dim(colors)
## [1] 27 31
  1. Print out the data observations for the 10 countries with the largest populations. The 10 data observations should be printed out in descending order according to population.
arrange(flag_df,desc(population)) %>%
  head(10)
## # A tibble: 10 x 31
##        X name  landmass zone   area population language religion  bars
##    <int> <fct> <fct>    <fct> <int>      <int> <fct>    <fct>    <int>
##  1    38 China 5        NE     9561       1008 7        6            0
##  2    82 India 5        NE     3268        684 6        NW           0
##  3   185 USSR  5        NE    22402        274 5        6            0
##  4   184 USA   NE       NW     9363        231 NE       NE           0
##  5    83 Indo~ 6        SE     1904        157 10       SE           0
##  6    24 Braz~ SE       SW     8512        119 6        0            0
##  7    91 Japan 5        NE      372        118 9        7            0
##  8    15 Bang~ 5        NE      143         90 6        SE           0
##  9   133 Paki~ 5        NE      804         84 6        SE           1
## 10   113 Mexi~ NE       NW     1973         77 SE       0            3
## # ... with 22 more variables: stripes <int>, colours <int>, red <lgl>,
## #   green <lgl>, blue <lgl>, gold <lgl>, white <lgl>, black <lgl>,
## #   orange <lgl>, mainhue <fct>, circles <int>, crosses <int>,
## #   saltires <int>, quarters <int>, sunstars <int>, crescent <lgl>,
## #   triangle <lgl>, icon <lgl>, animate <lgl>, text <lgl>, topleft <fct>,
## #   botright <fct>

Let’s see if we can find any patterns in the data.

  1. Group the flags by landmass and find the following for each group:

Your output should be a data frame with each row corresponding to a group. There will be five columns.

Repeat this process except group by zone, language, and religion.

# You may find this function useful (ie. you should call this function in your code)!  It calculates the mode of a factor.

cat_mode <- function(cat_var){
  mode_idx <- which.max(table(cat_var))
  levels(cat_var)[mode_idx]
}


# fill in your code here
flag_df %>%
  group_by(landmass) %>%
  summarise(ModeMainhue=cat_mode(mainhue),MedianLandmass=median(sunstars),animateLandmass=sum(animate), animateLandmassPercent=animateLandmass/(length(animate))*100)
## # A tibble: 6 x 5
##   landmass ModeMainhue MedianLandmass animateLandmass animateLandmassPerce~
##   <fct>    <chr>                <dbl>           <int>                 <dbl>
## 1 5        red                    1                 6                  15.4
## 2 6        blue                   2.5               6                  30  
## 3 NE       blue                   0                13                  41.9
## 4 NW       green                  0                 7                  13.5
## 5 SE       red                    0                 3                  17.6
## 6 SW       red                    0                 4                  11.4
flag_df %>%
  group_by(zone) %>%
  summarise(ModeZone=cat_mode(mainhue),MedianZone=median(sunstars),animateZone=sum(animate), animateZonePercent=animateZone/(length(animate))*100)
## # A tibble: 4 x 5
##   zone  ModeZone MedianZone animateZone animateZonePercent
##   <fct> <chr>         <dbl>       <int>              <dbl>
## 1 NE    red               0          14               15.4
## 2 NW    blue              0          15               25.9
## 3 SE    red               0           7               24.1
## 4 SW    blue              1           3               18.8
flag_df %>%
  group_by(language) %>%
  summarise(ModeLang=cat_mode(mainhue),MedianLang=median(sunstars),animateLang=sum(animate), animateLangPercent=animateLang/(length(animate))*100)
## # A tibble: 10 x 5
##    language ModeLang MedianLang animateLang animateLangPercent
##    <fct>    <chr>         <dbl>       <int>              <dbl>
##  1 10       red             0             9               19.6
##  2 5        red             0.5           1               25  
##  3 6        red             0             5               16.7
##  4 7        red             3             1               25  
##  5 8        red             0             2               10.5
##  6 9        red             0.5           0                0  
##  7 NE       blue            0            18               41.9
##  8 NW       red             0             0                0  
##  9 SE       blue            0             3               14.3
## 10 SW       gold            0             0                0
flag_df %>%
  group_by(religion) %>%
  summarise(ModeReligion=cat_mode(mainhue),MedianReligion=median(sunstars),animateReligion=sum(animate), animateReligionPercent=animateReligion/(length(animate))*100)
## # A tibble: 8 x 5
##   religion ModeReligion MedianReligion animateReligion animateReligionPerc~
##   <fct>    <chr>                 <dbl>           <int>                <dbl>
## 1 0        red                       0               4                10   
## 2 5        red                       0               6                22.2 
## 3 6        red                       1               3                20   
## 4 7        white                     1               0                 0   
## 5 NE       blue                      0              19                31.7 
## 6 NW       brown                     0               0                 0   
## 7 SE       red                       0               3                 8.33
## 8 SW       red                       0               4                50

Do you see any patterns in flag mainhue, sun or star symbols, and animate images? If so, describe these patterns. (Hint: you should see patterns! Look at the trends when grouping by landmass, zone, language, and religion.) Write a paragraph to answer this question.

When grouping by landmass, zone, language and religion, most of the NE regions have the highest number of animate objects and the highest percentage. Another pattern is that when there are sun or star symbols present, the number of animate objects is lowered.