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? None
flag_df <- read.csv("flag.csv")
is.data.frame(flag_df)
## [1] TRUE
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
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
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)
## 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.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
flag_df <- as_tibble(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.
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
What is in this first column? The counter number of rows/flags
Do we really need it? We do not really need it
flag_df <- subset(flag_df, select = -1 )
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"]<- "N.America"
flag_df$landmass[flag_df$landmass == "2"]<- "S.America"
flag_df$landmass[flag_df$landmass == "3"]<- "Europe"
flag_df$landmass[flag_df$landmass == "4"]<- "Africa"
flag_df$landmass[flag_df$landmass == "5"]<- "Asia"
flag_df$landmass[flag_df$landmass == "6"]<- "Oceania"
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"]<- "English"
flag_df$language[flag_df$language == "2"]<- "Spanish"
flag_df$language[flag_df$language == "3"]<- "French"
flag_df$language[flag_df$language == "4"]<- "German"
flag_df$language[flag_df$language == "5"]<- "Slavic"
flag_df$language[flag_df$language == "6"]<- "Other Indo-European"
flag_df$language[flag_df$language == "7"]<- "Chinese"
flag_df$language[flag_df$language == "8"]<- "Arabic"
flag_df$language[flag_df$language == "9"]<- "Japanese/Turkish/Finnish/Magyar"
flag_df$language[flag_df$language == "10"]<- "Others"
flag_df$religion<-as.character(flag_df$religion)
flag_df$religion[flag_df$religion == "0"]<- "Catholic"
flag_df$religion[flag_df$religion == "1"]<- "Other Christian"
flag_df$religion[flag_df$religion == "2"]<- "Muslim"
flag_df$religion[flag_df$religion == "3"]<- "Buddhist"
flag_df$religion[flag_df$religion == "4"]<- "Hindu"
flag_df$religion[flag_df$religion == "5"]<- "Ethnic"
flag_df$religion[flag_df$religion == "6"]<- "Marxist"
flag_df$religion[flag_df$religion == "7"]<- "Others"
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)
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
RBW_colors <- flag_df %>%
filter(red == TRUE & blue == TRUE & white == TRUE)
nrow(RBW_colors)
## [1] 63
RBW_only <- RBW_colors %>%
filter(red == TRUE & blue == TRUE & white == TRUE & black == FALSE & gold == FALSE & green == FALSE & orange == FALSE)
nrow(RBW_only)
## [1] 27
arrange(flag_df,desc(population)) %>%
head(10)
## # A tibble: 10 x 30
## name landmass zone area population language religion bars stripes colours
## <chr> <fct> <fct> <int> <int> <fct> <fct> <int> <int> <int>
## 1 China Asia NE 9561 1008 Chinese Marxist 0 0 2
## 2 India Asia NE 3268 684 Other I~ Hindu 0 3 4
## 3 USSR Asia NE 22402 274 Slavic Marxist 0 0 2
## 4 USA N.Ameri~ NW 9363 231 English Other C~ 0 13 3
## 5 Indo~ Oceania SE 1904 157 Others Muslim 0 2 2
## 6 Braz~ S.Ameri~ SW 8512 119 Other I~ Catholic 0 0 4
## 7 Japan Asia NE 372 118 Japanes~ Others 0 0 2
## 8 Bang~ Asia NE 143 90 Other I~ Muslim 0 0 2
## 9 Paki~ Asia NE 804 84 Other I~ Muslim 1 0 2
## 10 Mexi~ N.Ameri~ NW 1973 77 Spanish Catholic 3 0 4
## # ... with 20 more variables: 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]
}
flag_df %>%
group_by(landmass) %>%
summarise(ModeMainhue=cat_mode(mainhue),Median=median(sunstars),animateLandmass=sum(animate), LandmassPercent=animateLandmass/(length(animate))*100)
## # A tibble: 6 x 4
## landmass Median animateLandmass LandmassPercent
## <fct> <dbl> <int> <dbl>
## 1 Africa 0 7 13.5
## 2 Asia 1 6 15.4
## 3 Europe 0 4 11.4
## 4 N.America 0 13 41.9
## 5 Oceania 2.5 6 30
## 6 S.America 0 3 17.6
flag_df %>%
group_by(zone) %>%
summarise(ModeZone=cat_mode(mainhue),Median=median(sunstars),animateZone=sum(animate), ZonePercent=animateZone/(length(animate))*100)
## # A tibble: 4 x 4
## zone Median animateZone ZonePercent
## <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),Median=median(sunstars),animateLang=sum(animate), LanguagePercent=animateLang/(length(animate))*100)
## # A tibble: 10 x 4
## language Median animateLang LanguagePercent
## <fct> <dbl> <int> <dbl>
## 1 Arabic 0 2 10.5
## 2 Chinese 3 1 25
## 3 English 0 18 41.9
## 4 French 0 0 0
## 5 German 0 0 0
## 6 Japanese/Turkish/Finnish/Magyar 0.5 0 0
## 7 Other Indo-European 0 5 16.7
## 8 Others 0 9 19.6
## 9 Slavic 0.5 1 25
## 10 Spanish 0 3 14.3
flag_df %>%
group_by(religion) %>%
summarise(ModeReligion=cat_mode(mainhue),Median=median(sunstars),animateReligion=sum(animate), Percent=animateReligion/(length(animate))*100)
## # A tibble: 8 x 4
## religion Median animateReligion Percent
## <fct> <dbl> <int> <dbl>
## 1 Buddhist 0 4 50
## 2 Catholic 0 4 10
## 3 Ethnic 0 6 22.2
## 4 Hindu 0 0 0
## 5 Marxist 1 3 20
## 6 Muslim 0 3 8.33
## 7 Other Christian 0 19 31.7
## 8 Others 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.
N.America, NW, English language and Other Christian have the highest number of flags with animate images