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 flag.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.
class(flag_df)
## [1] "data.frame"
dim(flag_df)
## [1] 194  31
  1. Print out the first 5 lines and the last 5 lines of flag_df.
head(flag_df)
##   X           name landmass zone area population language religion bars stripes
## 1 1    Afghanistan        5    1  648         16       10        2    0       3
## 2 2        Albania        3    1   29          3        6        6    0       0
## 3 3        Algeria        4    1 2388         20        8        2    2       0
## 4 4 American-Samoa        6    3    0          0        1        1    0       0
## 5 5        Andorra        3    1    0          0        6        0    3       0
## 6 6         Angola        4    2 1247          7       10        5    0       2
##   colours red green blue gold white black orange mainhue circles crosses
## 1       5   1     1    0    1     1     1      0   green       0       0
## 2       3   1     0    0    1     0     1      0     red       0       0
## 3       3   1     1    0    0     1     0      0   green       0       0
## 4       5   1     0    1    1     1     0      1    blue       0       0
## 5       3   1     0    1    1     0     0      0    gold       0       0
## 6       3   1     0    0    1     0     1      0     red       0       0
##   saltires quarters sunstars crescent triangle icon animate text topleft
## 1        0        0        1        0        0    1       0    0   black
## 2        0        0        1        0        0    0       1    0     red
## 3        0        0        1        1        0    0       0    0   green
## 4        0        0        0        0        1    1       1    0    blue
## 5        0        0        0        0        0    0       0    0    blue
## 6        0        0        1        0        0    1       0    0     red
##   botright
## 1    green
## 2      red
## 3    white
## 4      red
## 5      red
## 6    black
tail(flag_df)
##       X          name landmass zone area population language religion bars
## 189 189       Vietnam        5    1  333         60       10        6    0
## 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
## 189       0       2   1     0    0    1     0     0      0     red       0
## 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
## 189       0        0        0        1        0        0    0       0    0
## 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
## 189     red      red
## 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   Length:194         Min.   :1.000   Min.   :1.000  
##  1st Qu.: 49.25   Class :character   1st Qu.:3.000   1st Qu.:1.000  
##  Median : 97.50   Mode  :character   Median :4.000   Median :2.000  
##  Mean   : 97.50                      Mean   :3.572   Mean   :2.211  
##  3rd Qu.:145.75                      3rd Qu.:5.000   3rd Qu.:4.000  
##  Max.   :194.00                      Max.   :6.000   Max.   :4.000  
##       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   Length:194         Min.   :0.0000  
##  1st Qu.:0.000   1st Qu.:0.000   Class :character   1st Qu.:0.0000  
##  Median :0.000   Median :0.000   Mode  :character   Median :0.0000  
##  Mean   :0.268   Mean   :0.134                      Mean   :0.1701  
##  3rd Qu.:1.000   3rd Qu.:0.000                      3rd Qu.:0.0000  
##  Max.   :1.000   Max.   :1.000                      Max.   :4.0000  
##     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   Length:194         Length:194        
##  1st Qu.:0.00000   Class :character   Class :character  
##  Median :0.00000   Mode  :character   Mode  :character  
##  Mean   :0.08247                                        
##  3rd Qu.:0.00000                                        
##  Max.   :1.00000
  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      : chr  "Afghanistan" "Albania" "Algeria" "American-Samoa" ...
##  $ 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   : chr  "green" "red" "green" "blue" ...
##  $ 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   : chr  "black" "red" "green" "blue" ...
##  $ botright  : chr  "green" "red" "white" "red" ...

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.3.2 ──
## ✔ ggplot2 3.4.0     ✔ purrr   1.0.1
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.3     ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
flag_df <- as_tibble(flag_df)
  1. Find the variable (column) names of flag_df.
colnames(flag_df)
##  [1] "X"          "name"       "landmass"   "zone"       "area"      
##  [6] "population" "language"   "religion"   "bars"       "stripes"   
## [11] "colours"    "red"        "green"      "blue"       "gold"      
## [16] "white"      "black"      "orange"     "mainhue"    "circles"   
## [21] "crosses"    "saltires"   "quarters"   "sunstars"   "crescent"  
## [26] "triangle"   "icon"       "animate"    "text"       "topleft"   
## [31] "botright"

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

  1. Print out the first column.
flag_df$X
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108
## [109] 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## [127] 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## [145] 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## [163] 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## [181] 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  1. Delete the first column of flag_df.
flag_df <- flag_df[, -1]
  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 <- factor(flag_df$landmass, levels = 1:6, labels = c("N.Am", "S.Am", "Europe", "Africa", "Asia", "Oceania"))
flag_df$zone <- factor(flag_df$zone, levels = 1:4, labels = c("NE", "SE", "SW", "NW"))
flag_df$language <- factor(flag_df$language, levels = 1:10, labels = c("English", "Spanish", "French", "German", "Slavic", "Other Indo-European", "Chinese", "Arabic", "Japanese/Turkish/Finnish/Magyar", "Others"))
flag_df$religion <- factor(flag_df$religion, levels = 0:7, labels = c("Catholic", "Other Christian", "Muslim", "Buddhist", "Hindu", "Ethnic", "Marxist", "Others"))

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?
rwb_flags <- flag_df[flag_df$red == 1 & flag_df$white == 1 & flag_df$blue == 1, ]
nrow(rwb_flags)
## [1] 63
only_red_white_blue_flags <- rwb_flags %>% 
  filter(green != 1 & gold != 1 & black != 1 & orange != 1)
nrow(only_red_white_blue_flags)
## [1] 27
  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.
top_10_pop <- head(flag_df[order(-flag_df$population), ], 10)
top_10_pop
## # A tibble: 10 × 30
##    name  landm…¹ zone   area popul…² langu…³ relig…⁴  bars stripes colours red  
##    <chr> <fct>   <fct> <int>   <int> <fct>   <fct>   <int>   <int>   <int> <lgl>
##  1 China Asia    NE     9561    1008 Chinese Marxist     0       0       2 TRUE 
##  2 India Asia    NE     3268     684 Other … Hindu       0       3       4 FALSE
##  3 USSR  Asia    NE    22402     274 Slavic  Marxist     0       0       2 TRUE 
##  4 USA   N.Am    NW     9363     231 English Other …     0      13       3 TRUE 
##  5 Indo… Oceania SE     1904     157 Others  Muslim      0       2       2 TRUE 
##  6 Braz… S.Am    SW     8512     119 Other … Cathol…     0       0       4 FALSE
##  7 Japan Asia    NE      372     118 Japane… Others      0       0       2 TRUE 
##  8 Bang… Asia    NE      143      90 Other … Muslim      0       0       2 TRUE 
##  9 Paki… Asia    NE      804      84 Other … Muslim      1       0       2 FALSE
## 10 Mexi… N.Am    NW     1973      77 Spanish Cathol…     3       0       4 TRUE 
## # … with 19 more variables: green <lgl>, blue <lgl>, gold <lgl>, white <lgl>,
## #   black <lgl>, orange <lgl>, mainhue <chr>, circles <int>, crosses <int>,
## #   saltires <int>, quarters <int>, sunstars <int>, crescent <lgl>,
## #   triangle <lgl>, icon <lgl>, animate <lgl>, text <lgl>, topleft <chr>,
## #   botright <chr>, and abbreviated variable names ¹​landmass, ²​population,
## #   ³​language, ⁴​religion

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]
}
landmass_summary <- flag_df %>%
  group_by(landmass) %>%
  summarise(
    mode_mainhue = cat_mode(mainhue),
    median_sunstars = median(sunstars),
    num_animate = sum(animate),
    perc_animate = mean(animate) * 100
  )
zone_summary <- flag_df %>%
  group_by(zone) %>%
  summarise(
    mode_mainhue = cat_mode(mainhue),
    median_sunstars = median(sunstars),
    num_animate = sum(animate),
    perc_animate = mean(animate) * 100
  )
lang_summary <- flag_df %>%
  group_by(language) %>%
  summarise(
    mode_mainhue = cat_mode(mainhue),
    median_sunstars = median(sunstars),
    num_animate = sum(animate),
    perc_animate = mean(animate) * 100
  )
religion_summary <-flag_df %>%
  group_by(religion) %>%
  summarise(
    mode_mainhue = cat_mode(mainhue),
    median_sunstars = median(sunstars),
    num_animate = sum(animate),
    perc_animate = mean(animate) * 100
  )

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.

It appears that the number of sun stars are are centralized within countries in Asia and Oceania, which primarily Marxist or a religion under the other category, and speak either Slavic, Chinese, or Japanese/Turkish/Finnish/Magyar. This is an interesting trend because the median of the rest of the regions, language, and religions are all zero, meaning these are the only parts of the world that use sun stars in there flags. I also notice that northern countries, especially in North America, tend to use way more animate images, which all heavily speak English and are either Catholic or other Christian in their religious beliefs. I find this trend intriguing because it is almost the exact opposite of the sun star trend, as seemingly polar opposite parts of the world that speak different languages and practice different religions have completely opposing design choices for their flags, with what appears to be little to no overlap at all.