Image by Gordon Johnson from Pixabay
Download flag.csv and flag.names to your working directory. Make sure to set your working directory appropriately!
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.
Who is the donor of this data? Richard S. Forsyth
Is there any missing data? No
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.1.3
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.7
## v tidyr 1.1.4 v stringr 1.4.0
## v readr 2.1.1 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
getwd()
## [1] "C:/Users/natha/OneDrive/Documents/Data 101"
setwd("~/Data 101")
flag_df <- read.csv("flag.csv")
library(tidyverse)
class(flag_df)
## [1] "data.frame"
dim(flag_df)
## [1] 194 31
head(flag_df, 5)
## 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
## 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
## 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
## botright
## 1 green
## 2 red
## 3 white
## 4 red
## 5 red
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
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" ...
We are going to use the dplyr package.
library(tidyverse)
flag_df<-as_tibble(flag_df)
flag_df %>% 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.
flag_df %>% select(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
What is in this first column? FILL IN YOUR ANSWER HERE
Do we really need it? FILL IN YOUR ANSWER HERE
select(flag_df , -X)
## # A tibble: 194 x 30
## name landmass zone area population language religion bars stripes colours
## <chr> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 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 <int>, green <int>,
## # blue <int>, gold <int>, white <int>, black <int>, orange <int>,
## # mainhue <chr>, circles <int>, crosses <int>, saltires <int>,
## # quarters <int>, sunstars <int>, crescent <int>, triangle <int>, icon <int>,
## # animate <int>, text <int>, topleft <chr>, botright <chr>
flag_df
## # A tibble: 194 x 31
## X name landmass zone area population language religion bars stripes
## <int> <chr> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 1 Afghan~ 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 Americ~ 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
## 7 7 Anguil~ 1 4 0 0 1 1 0 1
## 8 8 Antigu~ 1 4 0 0 1 1 0 1
## 9 9 Argent~ 2 3 2777 28 2 0 0 3
## 10 10 Argent~ 2 3 2777 28 2 0 0 3
## # ... with 184 more rows, and 21 more variables: colours <int>, red <int>,
## # green <int>, blue <int>, gold <int>, white <int>, black <int>,
## # orange <int>, mainhue <chr>, circles <int>, crosses <int>, saltires <int>,
## # quarters <int>, sunstars <int>, crescent <int>, triangle <int>, icon <int>,
## # animate <int>, text <int>, topleft <chr>, botright <chr>
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.
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.
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)
str(flag_df)
## tibble [194 x 31] (S3: tbl_df/tbl/data.frame)
## $ X : int [1:194] 1 2 3 4 5 6 7 8 9 10 ...
## $ name : chr [1:194] "Afghanistan" "Albania" "Algeria" "American-Samoa" ...
## $ landmass : Factor w/ 6 levels "5","6","NE","NW",..: 1 6 4 2 6 4 3 3 5 5 ...
## $ zone : Factor w/ 4 levels "NE","NW","SE",..: 1 1 1 4 1 3 2 2 4 4 ...
## $ area : int [1:194] 648 29 2388 0 0 1247 0 0 2777 2777 ...
## $ population: int [1:194] 16 3 20 0 0 7 0 0 28 28 ...
## $ language : Factor w/ 10 levels "10","5","6","7",..: 1 3 5 7 3 1 7 7 9 9 ...
## $ religion : Factor w/ 8 levels "0","5","6","7",..: 7 3 7 5 1 2 5 5 1 1 ...
## $ bars : int [1:194] 0 0 2 0 3 0 0 0 0 0 ...
## $ stripes : int [1:194] 3 0 0 0 0 2 1 1 3 3 ...
## $ colours : int [1:194] 5 3 3 5 3 3 3 5 2 3 ...
## $ red : logi [1:194] TRUE TRUE TRUE TRUE TRUE TRUE ...
## $ green : logi [1:194] TRUE FALSE TRUE FALSE FALSE FALSE ...
## $ blue : logi [1:194] FALSE FALSE FALSE TRUE TRUE FALSE ...
## $ gold : logi [1:194] TRUE TRUE FALSE TRUE TRUE TRUE ...
## $ white : logi [1:194] TRUE FALSE TRUE TRUE FALSE FALSE ...
## $ black : logi [1:194] TRUE TRUE FALSE FALSE FALSE TRUE ...
## $ orange : logi [1:194] FALSE FALSE FALSE TRUE FALSE FALSE ...
## $ mainhue : chr [1:194] "green" "red" "green" "blue" ...
## $ circles : int [1:194] 0 0 0 0 0 0 0 0 0 0 ...
## $ crosses : int [1:194] 0 0 0 0 0 0 0 0 0 0 ...
## $ saltires : int [1:194] 0 0 0 0 0 0 0 0 0 0 ...
## $ quarters : int [1:194] 0 0 0 0 0 0 0 0 0 0 ...
## $ sunstars : int [1:194] 1 1 1 0 0 1 0 1 0 1 ...
## $ crescent : logi [1:194] FALSE FALSE TRUE FALSE FALSE FALSE ...
## $ triangle : logi [1:194] FALSE FALSE FALSE TRUE FALSE FALSE ...
## $ icon : logi [1:194] TRUE FALSE FALSE TRUE FALSE TRUE ...
## $ animate : logi [1:194] FALSE TRUE FALSE TRUE FALSE FALSE ...
## $ text : logi [1:194] FALSE FALSE FALSE FALSE FALSE FALSE ...
## $ topleft : chr [1:194] "black" "red" "green" "blue" ...
## $ botright : chr [1:194] "green" "red" "white" "red" ...
Now that our data is clean, let’s answer some questions about it!
table(flag_df$mainhue)
##
## black blue brown gold green orange red white
## 5 40 2 19 31 4 71 22
library(dplyr)
RWB_ <- flag_df %>%
filter(red==TRUE|blue==TRUE|white==TRUE&black==FALSE&gold==FALSE&green==FALSE&orange==FALSE)
dim(RWB_)
## [1] 182 31
RWB <- flag_df %>%
filter(red==TRUE&blue==TRUE&white==TRUE&black==FALSE&gold==FALSE&green==FALSE&orange==FALSE)
dim(RWB)
## [1] 27 31
library(dplyr)
RWB_ <- flag_df %>%
filter(red==TRUE&blue==TRUE&white==TRUE&black==FALSE&gold==FALSE&green==FALSE&orange==FALSE)
dim(RWB_)
## [1] 27 31
arrange(flag_df,desc(population)) %>%
head(10)
## # A tibble: 10 x 31
## X name landmass zone area population language religion bars stripes
## <int> <chr> <fct> <fct> <int> <int> <fct> <fct> <int> <int>
## 1 38 China 5 NE 9561 1008 7 6 0 0
## 2 82 India 5 NE 3268 684 6 NW 0 3
## 3 185 USSR 5 NE 22402 274 5 6 0 0
## 4 184 USA NE NW 9363 231 NE NE 0 13
## 5 83 Indone~ 6 SE 1904 157 10 SE 0 2
## 6 24 Brazil SE SW 8512 119 6 0 0 0
## 7 91 Japan 5 NE 372 118 9 7 0 0
## 8 15 Bangla~ 5 NE 143 90 6 SE 0 0
## 9 133 Pakist~ 5 NE 804 84 6 SE 1 0
## 10 113 Mexico NE NW 1973 77 SE 0 3 0
## # ... with 21 more variables: colours <int>, red <lgl>, 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>
Let’s see if we can find any patterns in the data.
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 4
## landmass MedianLandmass animateLandmass animateLandmassPercent
## <fct> <dbl> <int> <dbl>
## 1 5 1 6 15.4
## 2 6 2.5 6 30
## 3 NE 0 13 41.9
## 4 NW 0 7 13.5
## 5 SE 0 3 17.6
## 6 SW 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 4
## zone MedianZone animateZone animateZonePercent
## <fct> <dbl> <int> <dbl>
## 1 NE 0 14 15.4
## 2 NW 0 15 25.9
## 3 SE 0 7 24.1
## 4 SW 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 4
## language MedianLang animateLang animateLangPercent
## <fct> <dbl> <int> <dbl>
## 1 10 0 9 19.6
## 2 5 0.5 1 25
## 3 6 0 5 16.7
## 4 7 3 1 25
## 5 8 0 2 10.5
## 6 9 0.5 0 0
## 7 NE 0 18 41.9
## 8 NW 0 0 0
## 9 SE 0 3 14.3
## 10 SW 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 4
## religion MedianReligion animateReligion animateReligionPercent
## <fct> <dbl> <int> <dbl>
## 1 0 0 4 10
## 2 5 0 6 22.2
## 3 6 1 3 20
## 4 7 1 0 0
## 5 NE 0 19 31.7
## 6 NW 0 0 0
## 7 SE 0 3 8.33
## 8 SW 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.
FILL IN YOUR ANSWER HERE
flag_df %>%
group_by(zone) %>%
summarise(Modesunstar=cat_mode(mainhue),Mediansunstar=median(sunstars),animatesunstar=sum(animate), animatesunstarPercent=animatesunstar/(length(animate))*100)
## # A tibble: 4 x 4
## zone Mediansunstar animatesunstar animatesunstarPercent
## <fct> <dbl> <int> <dbl>
## 1 NE 0 14 15.4
## 2 NW 0 15 25.9
## 3 SE 0 7 24.1
## 4 SW 1 3 18.8