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   1.0.0     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()
getwd()
## [1] "C:/Users/chat5/OneDrive/Desktop/Data 101/project2a files"
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')
## Warning: Missing column names filled in: 'X1' [1]
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   name = col_character(),
##   mainhue = col_character(),
##   topleft = col_character(),
##   botright = col_character()
## )
## See spec(...) for full column specifications.
  1. Check to make sure the class of flag_df is data.frame. Then find the dimensions of flag_df.
class(flag_df)
## [1] "spec_tbl_df" "tbl_df"      "tbl"         "data.frame"
dim(flag_df) # Dimensions are 194 rows by 31 columns.
## [1] 194  31
  1. Print out the first 5 lines and the last 5 lines of flag_df.
head(flag_df, 5)
## # A tibble: 5 x 31
##      X1 name  landmass  zone  area population language religion  bars
##   <dbl> <chr>    <dbl> <dbl> <dbl>      <dbl>    <dbl>    <dbl> <dbl>
## 1     1 Afgh~        5     1   648         16       10        2     0
## 2     2 Alba~        3     1    29          3        6        6     0
## 3     3 Alge~        4     1  2388         20        8        2     2
## 4     4 Amer~        6     3     0          0        1        1     0
## 5     5 Ando~        3     1     0          0        6        0     3
## # ... with 22 more variables: stripes <dbl>, 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)
##        X1             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)
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 194 obs. of  31 variables:
##  $ X1        : num  1 2 3 4 5 6 7 8 9 10 ...
##  $ name      : chr  "Afghanistan" "Albania" "Algeria" "American-Samoa" ...
##  $ landmass  : num  5 3 4 6 3 4 1 1 2 2 ...
##  $ zone      : num  1 1 1 3 1 2 4 4 3 3 ...
##  $ area      : num  648 29 2388 0 0 ...
##  $ population: num  16 3 20 0 0 7 0 0 28 28 ...
##  $ language  : num  10 6 8 1 6 10 1 1 2 2 ...
##  $ religion  : num  2 6 2 1 0 5 1 1 0 0 ...
##  $ bars      : num  0 0 2 0 3 0 0 0 0 0 ...
##  $ stripes   : num  3 0 0 0 0 2 1 1 3 3 ...
##  $ colours   : num  5 3 3 5 3 3 3 5 2 3 ...
##  $ red       : num  1 1 1 1 1 1 0 1 0 0 ...
##  $ green     : num  1 0 1 0 0 0 0 0 0 0 ...
##  $ blue      : num  0 0 0 1 1 0 1 1 1 1 ...
##  $ gold      : num  1 1 0 1 1 1 0 1 0 1 ...
##  $ white     : num  1 0 1 1 0 0 1 1 1 1 ...
##  $ black     : num  1 1 0 0 0 1 0 1 0 0 ...
##  $ orange    : num  0 0 0 1 0 0 1 0 0 0 ...
##  $ mainhue   : chr  "green" "red" "green" "blue" ...
##  $ circles   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ crosses   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ saltires  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ quarters  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ sunstars  : num  1 1 1 0 0 1 0 1 0 1 ...
##  $ crescent  : num  0 0 1 0 0 0 0 0 0 0 ...
##  $ triangle  : num  0 0 0 1 0 0 0 1 0 0 ...
##  $ icon      : num  1 0 0 1 0 1 0 0 0 0 ...
##  $ animate   : num  0 1 0 1 0 0 1 0 0 0 ...
##  $ text      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ topleft   : chr  "black" "red" "green" "blue" ...
##  $ botright  : chr  "green" "red" "white" "red" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   X1 = 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()
##   .. )

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)

flag_tibble <- as_tibble(flag_df)

class(flag_tibble)
## [1] "tbl_df"     "tbl"        "data.frame"
  1. Find the variable (column) names of flag_df.
flag_tibble %>%  select(name)
## # A tibble: 194 x 1
##    name           
##    <chr>          
##  1 Afghanistan    
##  2 Albania        
##  3 Algeria        
##  4 American-Samoa 
##  5 Andorra        
##  6 Angola         
##  7 Anguilla       
##  8 Antigua-Barbuda
##  9 Argentina      
## 10 Argentine      
## # ... with 184 more rows

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

  1. Print out the first column.
flag_tibble %>%  select(1)
## # A tibble: 194 x 1
##       X1
##    <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_tibble = subset(flag_tibble, select = -c(X1) )
  1. Verify that there are no missing values in flag_df.
flag_tibble
## # A tibble: 194 x 30
##    name  landmass  zone  area population language religion  bars stripes
##    <chr>    <dbl> <dbl> <dbl>      <dbl>    <dbl>    <dbl> <dbl>   <dbl>
##  1 Afgh~        5     1   648         16       10        2     0       3
##  2 Alba~        3     1    29          3        6        6     0       0
##  3 Alge~        4     1  2388         20        8        2     2       0
##  4 Amer~        6     3     0          0        1        1     0       0
##  5 Ando~        3     1     0          0        6        0     3       0
##  6 Ango~        4     2  1247          7       10        5     0       2
##  7 Angu~        1     4     0          0        1        1     0       1
##  8 Anti~        1     4     0          0        1        1     0       1
##  9 Arge~        2     3  2777         28        2        0     0       3
## 10 Arge~        2     3  2777         28        2        0     0       3
## # ... with 184 more rows, and 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>

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_tibble <- flag_tibble %>% mutate(landmass = replace(landmass, landmass == '1', "N.America")) %>%  mutate(landmass = replace(landmass, landmass == '2', "S.America")) %>%
   mutate(landmass = replace(landmass, landmass == '3', "Europe")) %>%
   mutate(landmass = replace(landmass, landmass == '4', "Africa")) %>%
   mutate(landmass = replace(landmass, landmass == '5', "Asia")) %>%
   mutate(landmass = replace(landmass, landmass == '6', "Oceania")) 

flag_tibble <- flag_tibble %>% mutate(zone = replace(zone, zone == '1', "NE")) %>%  mutate(zone = replace(zone, zone == '2', "SE")) %>%
   mutate(zone = replace(zone, zone == '3', "SW")) %>%
   mutate(zone = replace(zone, zone == '4', "NW"))

flag_tibble <- flag_tibble %>% mutate(language = replace(language, language == '1', "English")) %>%  mutate(language = replace(language, language == '2', "Spanish")) %>%
   mutate(language = replace(language, language == '3', "French")) %>%
   mutate(language = replace(language, language == '4', "German")) %>%
   mutate(language = replace(language, language == '5', "Slavic")) %>%
   mutate(language = replace(language, language == '6', "Other, Indo-European")) %>% 
   mutate(language = replace(language, language == '7', "Chinese")) %>%
   mutate(language = replace(language, language == '8', "Arabic")) %>%
   mutate(language = replace(language, language == '9', "Japanese/Turkish/Finnish/Magyar")) %>%
   mutate(language = replace(language, language == '10', "Others")) 

flag_tibble <- flag_tibble %>% mutate(religion = replace(religion, religion == '1', "English")) %>%  mutate(religion = replace(religion, religion == '2', "Spanish")) %>%
   mutate(religion = replace(religion, religion == '3', "French")) %>%
   mutate(religion = replace(religion, religion == '4', "German")) %>%
   mutate(religion = replace(religion, religion == '5', "Slavic")) %>%
   mutate(religion = replace(religion, religion == '6', "Other, Indo-European")) %>% 
   mutate(religion = replace(religion, religion == '7', "Chinese"))

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_tibble$red <- as.logical(flag_tibble$red)   
flag_tibble$green <- as.logical(flag_tibble$green)
flag_tibble$blue <- as.logical(flag_tibble$blue)
flag_tibble$gold <- as.logical(flag_tibble$gold)
flag_tibble$white <- as.logical(flag_tibble$white)
flag_tibble$black <- as.logical(flag_tibble$black)
flag_tibble$orange <- as.logical(flag_tibble$orange)
flag_tibble$crescent <- as.logical(flag_tibble$crescent)
flag_tibble$triangle <- as.logical(flag_tibble$triangle) 
flag_tibble$icon <- as.logical(flag_tibble$icon) 
flag_tibble$animate <- as.logical(flag_tibble$animate) 
flag_tibble$text <- as.logical(flag_tibble$text)



str(flag_tibble)
## Classes 'tbl_df', 'tbl' and 'data.frame':    194 obs. of  30 variables:
##  $ name      : chr  "Afghanistan" "Albania" "Algeria" "American-Samoa" ...
##  $ landmass  : chr  "Asia" "Europe" "Africa" "Oceania" ...
##  $ zone      : chr  "NE" "NE" "NE" "SW" ...
##  $ area      : num  648 29 2388 0 0 ...
##  $ population: num  16 3 20 0 0 7 0 0 28 28 ...
##  $ language  : chr  "Others" "Other, Indo-European" "Arabic" "English" ...
##  $ religion  : chr  "Spanish" "Other, Indo-European" "Spanish" "English" ...
##  $ bars      : num  0 0 2 0 3 0 0 0 0 0 ...
##  $ stripes   : num  3 0 0 0 0 2 1 1 3 3 ...
##  $ colours   : num  5 3 3 5 3 3 3 5 2 3 ...
##  $ red       : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
##  $ green     : logi  TRUE FALSE TRUE FALSE FALSE FALSE ...
##  $ blue      : logi  FALSE FALSE FALSE TRUE TRUE FALSE ...
##  $ gold      : logi  TRUE TRUE FALSE TRUE TRUE TRUE ...
##  $ white     : logi  TRUE FALSE TRUE TRUE FALSE FALSE ...
##  $ black     : logi  TRUE TRUE FALSE FALSE FALSE TRUE ...
##  $ orange    : logi  FALSE FALSE FALSE TRUE FALSE FALSE ...
##  $ mainhue   : chr  "green" "red" "green" "blue" ...
##  $ circles   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ crosses   : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ saltires  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ quarters  : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ sunstars  : num  1 1 1 0 0 1 0 1 0 1 ...
##  $ crescent  : logi  FALSE FALSE TRUE FALSE FALSE FALSE ...
##  $ triangle  : logi  FALSE FALSE FALSE TRUE FALSE FALSE ...
##  $ icon      : logi  TRUE FALSE FALSE TRUE FALSE TRUE ...
##  $ animate   : logi  FALSE TRUE FALSE TRUE FALSE FALSE ...
##  $ text      : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
##  $ topleft   : chr  "black" "red" "green" "blue" ...
##  $ botright  : chr  "green" "red" "white" "red" ...

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_tibble$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?
X1 <- filter(flag_tibble, red, white, blue)

X1 # 11 rows present, so 11 countries have red, white and blue in their flag.
## # A tibble: 63 x 30
##    name  landmass zone   area population language religion  bars stripes
##    <chr> <chr>    <chr> <dbl>      <dbl> <chr>    <chr>    <dbl>   <dbl>
##  1 Amer~ Oceania  SW        0          0 English  English      0       0
##  2 Anti~ N.Ameri~ NW        0          0 English  English      0       1
##  3 Aust~ Oceania  SE     7690         15 English  English      0       0
##  4 Beli~ N.Ameri~ NW       23          0 English  English      0       2
##  5 Berm~ N.Ameri~ NW        0          0 English  English      0       0
##  6 Brit~ N.Ameri~ NW        0          0 English  English      0       0
##  7 Bulg~ Europe   NE      111          9 Slavic   Other, ~     0       3
##  8 Burma Asia     NE      678         35 Others   French       0       0
##  9 Caym~ N.Ameri~ NW        0          0 English  English      0       0
## 10 Cent~ Africa   NE      623          2 Others   Slavic       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>
X2 <- filter(X1, green == FALSE)

X2 # There are no flags that have only red, white and blue in their flags. 
## # A tibble: 39 x 30
##    name  landmass zone   area population language religion  bars stripes
##    <chr> <chr>    <chr> <dbl>      <dbl> <chr>    <chr>    <dbl>   <dbl>
##  1 Amer~ Oceania  SW        0          0 English  English      0       0
##  2 Anti~ N.Ameri~ NW        0          0 English  English      0       1
##  3 Aust~ Oceania  SE     7690         15 English  English      0       0
##  4 Burma Asia     NE      678         35 Others   French       0       0
##  5 Chile S.Ameri~ SW      757         11 Spanish  0            0       2
##  6 Cook~ Oceania  SW        0          0 English  English      0       0
##  7 Cost~ N.Ameri~ NW       51          2 Spanish  0            0       5
##  8 Cuba  N.Ameri~ NW      115         10 Spanish  Other, ~     0       5
##  9 Czec~ Europe   NE      128         15 Slavic   Other, ~     0       0
## 10 Domi~ N.Ameri~ NW       49          6 Spanish  0            0       0
## # ... with 29 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_tibble
## # A tibble: 194 x 30
##    name  landmass zone   area population language religion  bars stripes
##    <chr> <chr>    <chr> <dbl>      <dbl> <chr>    <chr>    <dbl>   <dbl>
##  1 Afgh~ Asia     NE      648         16 Others   Spanish      0       3
##  2 Alba~ Europe   NE       29          3 Other, ~ Other, ~     0       0
##  3 Alge~ Africa   NE     2388         20 Arabic   Spanish      2       0
##  4 Amer~ Oceania  SW        0          0 English  English      0       0
##  5 Ando~ Europe   NE        0          0 Other, ~ 0            3       0
##  6 Ango~ Africa   SE     1247          7 Others   Slavic       0       2
##  7 Angu~ N.Ameri~ NW        0          0 English  English      0       1
##  8 Anti~ N.Ameri~ NW        0          0 English  English      0       1
##  9 Arge~ S.Ameri~ SW     2777         28 Spanish  0            0       3
## 10 Arge~ S.Ameri~ SW     2777         28 Spanish  0            0       3
## # ... with 184 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_tibblepop <-arrange(flag_tibble, desc(population))

head(flag_tibblepop, 10)
## # A tibble: 10 x 30
##    name  landmass zone   area population language religion  bars stripes
##    <chr> <chr>    <chr> <dbl>      <dbl> <chr>    <chr>    <dbl>   <dbl>
##  1 China Asia     NE     9561       1008 Chinese  Other, ~     0       0
##  2 India Asia     NE     3268        684 Other, ~ German       0       3
##  3 USSR  Asia     NE    22402        274 Slavic   Other, ~     0       0
##  4 USA   N.Ameri~ NW     9363        231 English  English      0      13
##  5 Indo~ Oceania  SE     1904        157 Others   Spanish      0       2
##  6 Braz~ S.Ameri~ SW     8512        119 Other, ~ 0            0       0
##  7 Japan Asia     NE      372        118 Japanes~ Chinese      0       0
##  8 Bang~ Asia     NE      143         90 Other, ~ Spanish      0       0
##  9 Paki~ Asia     NE      804         84 Other, ~ Spanish      1       0
## 10 Mexi~ N.Ameri~ NW     1973         77 Spanish  0            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]
}

library(tidyverse)

flagzone <- flag_tibble %>% group_by(zone) 

# median(flagzone)

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.

FILL IN YOUR ANSWER HERE