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.
library(readr)
flag_df <- read_csv("~/Data101/Project2a/project2a files/flag.csv")
## New names:
## * `` -> ...1
## Rows: 194 Columns: 31
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (4): name, mainhue, topleft, botright
## dbl (27): ...1, landmass, zone, area, population, language, religion, bars, ...
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
  1. Check to make sure the class of flag_df is data.frame. Then find the dimensions of flag_df.
is.data.frame(flag_df)
## [1] TRUE
  1. Print out the first 5 lines and the last 5 lines of flag_df.
head(flag_df)
## # A tibble: 6 x 31
##    ...1 name     landmass  zone  area population language religion  bars stripes
##   <dbl> <chr>       <dbl> <dbl> <dbl>      <dbl>    <dbl>    <dbl> <dbl>   <dbl>
## 1     1 Afghani~        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 America~        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
## # ... with 21 more variables: colours <dbl>, red <dbl>, green <dbl>,
## #   blue <dbl>, gold <dbl>, white <dbl>, black <dbl>, orange <dbl>,
## #   mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <dbl>, triangle <dbl>, icon <dbl>,
## #   animate <dbl>, text <dbl>, topleft <chr>, botright <chr>
tail(flag_df)
## # A tibble: 6 x 31
##    ...1 name     landmass  zone  area population language religion  bars stripes
##   <dbl> <chr>       <dbl> <dbl> <dbl>      <dbl>    <dbl>    <dbl> <dbl>   <dbl>
## 1   189 Vietnam         5     1   333         60       10        6     0       0
## 2   190 Western~        6     3     3          0        1        1     0       0
## 3   191 Yugosla~        3     1   256         22        6        6     0       3
## 4   192 Zaire           4     2   905         28       10        5     0       0
## 5   193 Zambia          4     2   753          6       10        5     3       0
## 6   194 Zimbabwe        4     2   391          8       10        5     0       7
## # ... with 21 more variables: colours <dbl>, red <dbl>, green <dbl>,
## #   blue <dbl>, gold <dbl>, white <dbl>, black <dbl>, orange <dbl>,
## #   mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <dbl>, triangle <dbl>, icon <dbl>,
## #   animate <dbl>, text <dbl>, topleft <chr>, botright <chr>
  1. Print out the summary statistics of each variable of flag_df.
summary(flag_df)
##       ...1            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)
## spec_tbl_df [194 x 31] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ ...1      : num [1:194] 1 2 3 4 5 6 7 8 9 10 ...
##  $ name      : chr [1:194] "Afghanistan" "Albania" "Algeria" "American-Samoa" ...
##  $ landmass  : num [1:194] 5 3 4 6 3 4 1 1 2 2 ...
##  $ zone      : num [1:194] 1 1 1 3 1 2 4 4 3 3 ...
##  $ area      : num [1:194] 648 29 2388 0 0 ...
##  $ population: num [1:194] 16 3 20 0 0 7 0 0 28 28 ...
##  $ language  : num [1:194] 10 6 8 1 6 10 1 1 2 2 ...
##  $ religion  : num [1:194] 2 6 2 1 0 5 1 1 0 0 ...
##  $ bars      : num [1:194] 0 0 2 0 3 0 0 0 0 0 ...
##  $ stripes   : num [1:194] 3 0 0 0 0 2 1 1 3 3 ...
##  $ colours   : num [1:194] 5 3 3 5 3 3 3 5 2 3 ...
##  $ red       : num [1:194] 1 1 1 1 1 1 0 1 0 0 ...
##  $ green     : num [1:194] 1 0 1 0 0 0 0 0 0 0 ...
##  $ blue      : num [1:194] 0 0 0 1 1 0 1 1 1 1 ...
##  $ gold      : num [1:194] 1 1 0 1 1 1 0 1 0 1 ...
##  $ white     : num [1:194] 1 0 1 1 0 0 1 1 1 1 ...
##  $ black     : num [1:194] 1 1 0 0 0 1 0 1 0 0 ...
##  $ orange    : num [1:194] 0 0 0 1 0 0 1 0 0 0 ...
##  $ mainhue   : chr [1:194] "green" "red" "green" "blue" ...
##  $ circles   : num [1:194] 0 0 0 0 0 0 0 0 0 0 ...
##  $ crosses   : num [1:194] 0 0 0 0 0 0 0 0 0 0 ...
##  $ saltires  : num [1:194] 0 0 0 0 0 0 0 0 0 0 ...
##  $ quarters  : num [1:194] 0 0 0 0 0 0 0 0 0 0 ...
##  $ sunstars  : num [1:194] 1 1 1 0 0 1 0 1 0 1 ...
##  $ crescent  : num [1:194] 0 0 1 0 0 0 0 0 0 0 ...
##  $ triangle  : num [1:194] 0 0 0 1 0 0 0 1 0 0 ...
##  $ icon      : num [1:194] 1 0 0 1 0 1 0 0 0 0 ...
##  $ animate   : num [1:194] 0 1 0 1 0 0 1 0 0 0 ...
##  $ text      : num [1:194] 0 0 0 0 0 0 0 0 0 0 ...
##  $ topleft   : chr [1:194] "black" "red" "green" "blue" ...
##  $ botright  : chr [1:194] "green" "red" "white" "red" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   ...1 = col_double(),
##   ..   name = col_character(),
##   ..   landmass = col_double(),
##   ..   zone = col_double(),
##   ..   area = col_double(),
##   ..   population = col_double(),
##   ..   language = col_double(),
##   ..   religion = col_double(),
##   ..   bars = col_double(),
##   ..   stripes = col_double(),
##   ..   colours = col_double(),
##   ..   red = col_double(),
##   ..   green = col_double(),
##   ..   blue = col_double(),
##   ..   gold = col_double(),
##   ..   white = col_double(),
##   ..   black = col_double(),
##   ..   orange = col_double(),
##   ..   mainhue = col_character(),
##   ..   circles = col_double(),
##   ..   crosses = col_double(),
##   ..   saltires = col_double(),
##   ..   quarters = col_double(),
##   ..   sunstars = col_double(),
##   ..   crescent = col_double(),
##   ..   triangle = col_double(),
##   ..   icon = col_double(),
##   ..   animate = col_double(),
##   ..   text = col_double(),
##   ..   topleft = col_character(),
##   ..   botright = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>

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.1 --
## v ggplot2 3.3.5     v dplyr   1.0.8
## v tibble  3.1.6     v stringr 1.4.0
## v tidyr   1.2.0     v forcats 0.5.1
## v purrr   0.3.4
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
  1. Find the variable (column) names of flag_df.
colnames(flag_df)
##  [1] "...1"       "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.
library(dplyr)

flag_df %>%
  select(c("...1"))
## # A tibble: 194 x 1
##     ...1
##    <dbl>
##  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 %>%
  select(-...1)
## # A tibble: 194 x 30
##    name  landmass  zone  area population language religion  bars stripes colours
##    <chr>    <dbl> <dbl> <dbl>      <dbl>    <dbl>    <dbl> <dbl>   <dbl>   <dbl>
##  1 Afgh~        5     1   648         16       10        2     0       3       5
##  2 Alba~        3     1    29          3        6        6     0       0       3
##  3 Alge~        4     1  2388         20        8        2     2       0       3
##  4 Amer~        6     3     0          0        1        1     0       0       5
##  5 Ando~        3     1     0          0        6        0     3       0       3
##  6 Ango~        4     2  1247          7       10        5     0       2       3
##  7 Angu~        1     4     0          0        1        1     0       1       3
##  8 Anti~        1     4     0          0        1        1     0       1       5
##  9 Arge~        2     3  2777         28        2        0     0       3       2
## 10 Arge~        2     3  2777         28        2        0     0       3       3
## # ... with 184 more rows, and 20 more variables: red <dbl>, green <dbl>,
## #   blue <dbl>, gold <dbl>, white <dbl>, black <dbl>, orange <dbl>,
## #   mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <dbl>, triangle <dbl>, icon <dbl>,
## #   animate <dbl>, text <dbl>, topleft <chr>, botright <chr>
  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= c(1,2,3,4,5,6),
                        labels= c("N.America", "S.America", "Europe", "Africa", "Asia", "Oceania"))

flag_df$zone <- factor(flag_df$zone,
                    levels= c(1,2,3,4),
                    labels= c("NE", "SE", "SW", "NW"))

flag_df$religion <- factor(flag_df$religion,
                        levels= c(0,1,2,3,4,5,6,7),
                        labels= c("Catholic", "Other Christian", "Muslim", "Buddhist", "Hindu", "Ethnic", "Marxist", "Other"))

flag_df$language <- factor(flag_df$language, 
                        levels= c(1,2,3,4,5,6,7,8,9,10),
                        labels= c("English", "Spanish", "French", "German", "Slavic", "Other Indo European", "Chinese", "Arabic", "Japanese/Turkish/Finnish/Magya", "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.
print(flag_df$mainhue)
##   [1] "green"  "red"    "green"  "blue"   "gold"   "red"    "white"  "red"   
##   [9] "blue"   "blue"   "blue"   "red"    "blue"   "red"    "green"  "blue"  
##  [17] "gold"   "blue"   "green"  "red"    "orange" "red"    "blue"   "green" 
##  [25] "blue"   "gold"   "red"    "red"    "red"    "red"    "gold"   "red"   
##  [33] "gold"   "blue"   "gold"   "gold"   "red"    "red"    "gold"   "green" 
##  [41] "red"    "blue"   "blue"   "blue"   "white"  "white"  "red"    "blue"  
##  [49] "green"  "blue"   "gold"   "black"  "blue"   "green"  "green"  "white" 
##  [57] "blue"   "blue"   "white"  "white"  "white"  "red"    "green"  "red"   
##  [65] "gold"   "black"  "red"    "white"  "blue"   "white"  "gold"   "blue"  
##  [73] "blue"   "gold"   "gold"   "green"  "black"  "blue"   "blue"   "red"   
##  [81] "blue"   "orange" "red"    "red"    "red"    "white"  "white"  "white" 
##  [89] "white"  "green"  "white"  "black"  "red"    "red"    "red"    "green" 
##  [97] "red"    "red"    "blue"   "red"    "green"  "red"    "red"    "red"   
## [105] "red"    "red"    "red"    "gold"   "red"    "blue"   "green"  "red"   
## [113] "green"  "blue"   "red"    "red"    "blue"   "red"    "gold"   "blue"  
## [121] "brown"  "red"    "white"  "blue"   "blue"   "orange" "green"  "gold"  
## [129] "blue"   "red"    "red"    "red"    "green"  "red"    "black"  "red"   
## [137] "red"    "blue"   "white"  "red"    "red"    "brown"  "red"    "red"   
## [145] "white"  "green"  "green"  "green"  "red"    "green"  "white"  "green" 
## [153] "blue"   "orange" "white"  "red"    "red"    "gold"   "blue"   "green" 
## [161] "blue"   "green"  "red"    "red"    "blue"   "blue"   "red"    "red"   
## [169] "red"    "green"  "red"    "green"  "red"    "red"    "red"    "red"   
## [177] "blue"   "blue"   "green"  "gold"   "red"    "white"  "white"  "white" 
## [185] "red"    "red"    "gold"   "red"    "red"    "red"    "red"    "green" 
## [193] "green"  "green"
  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?

63 countries have red, white, and blue in their flags.

flag_df %>%
  filter(red == TRUE & blue == TRUE & white == TRUE)
## # A tibble: 63 x 31
##     ...1 name    landmass zone   area population language religion  bars stripes
##    <dbl> <chr>   <fct>    <fct> <dbl>      <dbl> <fct>    <fct>    <dbl>   <dbl>
##  1     4 Americ~ Oceania  SW        0          0 English  Other C~     0       0
##  2     8 Antigu~ N.Ameri~ NW        0          0 English  Other C~     0       1
##  3    11 Austra~ Oceania  SE     7690         15 English  Other C~     0       0
##  4    18 Belize  N.Ameri~ NW       23          0 English  Other C~     0       2
##  5    20 Bermuda N.Ameri~ NW        0          0 English  Other C~     0       0
##  6    25 Britis~ N.Ameri~ NW        0          0 English  Other C~     0       0
##  7    27 Bulgar~ Europe   NE      111          9 Slavic   Marxist      0       3
##  8    29 Burma   Asia     NE      678         35 Others   Buddhist     0       0
##  9    34 Cayman~ N.Ameri~ NW        0          0 English  Other C~     0       0
## 10    35 Centra~ Africa   NE      623          2 Others   Ethnic       1       0
## # ... with 53 more rows, and 21 more variables: colours <dbl>, red <lgl>,
## #   green <lgl>, blue <lgl>, gold <lgl>, white <lgl>, black <lgl>,
## #   orange <lgl>, mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <lgl>, triangle <lgl>, icon <lgl>,
## #   animate <lgl>, text <lgl>, topleft <chr>, botright <chr>
flag_df %>%
  filter(red== TRUE & blue== TRUE & white == TRUE & green == FALSE & black == FALSE & gold == FALSE)
## # A tibble: 27 x 31
##     ...1 name    landmass zone   area population language religion  bars stripes
##    <dbl> <chr>   <fct>    <fct> <dbl>      <dbl> <fct>    <fct>    <dbl>   <dbl>
##  1    11 Austra~ Oceania  SE     7690         15 English  Other C~     0       0
##  2    29 Burma   Asia     NE      678         35 Others   Buddhist     0       0
##  3    37 Chile   S.Ameri~ SW      757         11 Spanish  Catholic     0       2
##  4    42 Cook-I~ Oceania  SW        0          0 English  Other C~     0       0
##  5    43 Costa-~ N.Ameri~ NW       51          2 Spanish  Catholic     0       5
##  6    44 Cuba    N.Ameri~ NW      115         10 Spanish  Marxist      0       5
##  7    46 Czecho~ Europe   NE      128         15 Slavic   Marxist      0       0
##  8    50 Domini~ N.Ameri~ NW       49          6 Spanish  Catholic     0       0
##  9    56 Faeroes Europe   NW        1          0 Other I~ Other C~     0       0
## 10    60 France  Europe   NE      547         54 French   Catholic     3       0
## # ... with 17 more rows, and 21 more variables: colours <dbl>, red <lgl>,
## #   green <lgl>, blue <lgl>, gold <lgl>, white <lgl>, black <lgl>,
## #   orange <lgl>, mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <lgl>, triangle <lgl>, icon <lgl>,
## #   animate <lgl>, text <lgl>, topleft <chr>, botright <chr>
  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.
flag_df %>%
    arrange(desc(population)) %>%
    slice(1:10)
## # A tibble: 10 x 31
##     ...1 name    landmass zone   area population language religion  bars stripes
##    <dbl> <chr>   <fct>    <fct> <dbl>      <dbl> <fct>    <fct>    <dbl>   <dbl>
##  1    38 China   Asia     NE     9561       1008 Chinese  Marxist      0       0
##  2    82 India   Asia     NE     3268        684 Other I~ Hindu        0       3
##  3   185 USSR    Asia     NE    22402        274 Slavic   Marxist      0       0
##  4   184 USA     N.Ameri~ NW     9363        231 English  Other C~     0      13
##  5    83 Indone~ Oceania  SE     1904        157 Others   Muslim       0       2
##  6    24 Brazil  S.Ameri~ SW     8512        119 Other I~ Catholic     0       0
##  7    91 Japan   Asia     NE      372        118 Japanes~ Other        0       0
##  8    15 Bangla~ Asia     NE      143         90 Other I~ Muslim       0       0
##  9   133 Pakist~ Asia     NE      804         84 Other I~ Muslim       1       0
## 10   113 Mexico  N.Ameri~ NW     1973         77 Spanish  Catholic     3       0
## # ... with 21 more variables: colours <dbl>, red <lgl>, green <lgl>,
## #   blue <lgl>, gold <lgl>, white <lgl>, black <lgl>, orange <lgl>,
## #   mainhue <chr>, circles <dbl>, crosses <dbl>, saltires <dbl>,
## #   quarters <dbl>, sunstars <dbl>, crescent <lgl>, triangle <lgl>, icon <lgl>,
## #   animate <lgl>, text <lgl>, topleft <chr>, botright <chr>

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]
}


flag_df$mainhue <- as.factor(flag_df$mainhue)
flag_dfLM <- group_by(flag_df, landmass)
summarise(flag_dfLM, mode_color = cat_mode(mainhue),
          sun_star = median(sunstars),
          animate_count = sum(animate),
          animate_pct = round(100 * animate_count / n(), digits = 2))
## # A tibble: 6 x 5
##   landmass  mode_color sun_star animate_count animate_pct
##   <fct>     <chr>         <dbl>         <int>       <dbl>
## 1 N.America blue            0              13        41.9
## 2 S.America red             0               3        17.6
## 3 Europe    red             0               4        11.4
## 4 Africa    green           0               7        13.5
## 5 Asia      red             1               6        15.4
## 6 Oceania   blue            2.5             6        30
flag_dfZ <- group_by(flag_df, zone)
summarise(flag_dfZ, mode_color=cat_mode(mainhue),
          sun_star=median(sunstars),
          animate_count=sum(animate),
          animate_pct=100*animate_count/n())
## # A tibble: 4 x 5
##   zone  mode_color sun_star animate_count animate_pct
##   <fct> <chr>         <dbl>         <int>       <dbl>
## 1 NE    red               0            14        15.4
## 2 SE    red               0             7        24.1
## 3 SW    blue              1             3        18.8
## 4 NW    blue              0            15        25.9
flag_dfLAN <- group_by(flag_df, language)
summarise(flag_dfLAN, mode_color=cat_mode(mainhue),
          sun_star=median(sunstars),
          animate_count=sum(animate),
          animate_pct=100*animate_count/n())
## # A tibble: 10 x 5
##    language                       mode_color sun_star animate_count animate_pct
##    <fct>                          <chr>         <dbl>         <int>       <dbl>
##  1 English                        blue            0              18        41.9
##  2 Spanish                        blue            0               3        14.3
##  3 French                         gold            0               0         0  
##  4 German                         red             0               0         0  
##  5 Slavic                         red             0.5             1        25  
##  6 Other Indo European            red             0               5        16.7
##  7 Chinese                        red             3               1        25  
##  8 Arabic                         red             0               2        10.5
##  9 Japanese/Turkish/Finnish/Magya red             0.5             0         0  
## 10 Others                         red             0               9        19.6
flag_dfRL <- group_by(flag_df, religion)
summarise(flag_dfRL, mode_color=cat_mode(mainhue),
          sun_star=median(sunstars),
          animate_count=sum(animate),
          animate_pct=100*animate_count/n())
## # A tibble: 8 x 5
##   religion        mode_color sun_star animate_count animate_pct
##   <fct>           <chr>         <dbl>         <int>       <dbl>
## 1 Catholic        red               0             4       10   
## 2 Other Christian blue              0            19       31.7 
## 3 Muslim          red               0             3        8.33
## 4 Buddhist        red               0             4       50   
## 5 Hindu           brown             0             0        0   
## 6 Ethnic          red               0             6       22.2 
## 7 Marxist         red               1             3       20   
## 8 Other           white             1             0        0

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.

Here goes nothing…

I see a pattern with the color BLUE and animate images being popular across landmass, zone, religion, and language. In landmass column , you can see the mode color (blue) and animated images count is 13. In the zone column, the mode color (blue) in the NW row has an an animated count of 15. In the language column, the mode color (blue) has an animated count of 18 and in the religion column the mode color (blue) has an animate count of 19.