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)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
setwd("/Users/smhenderson/Desktop/DATA101/R/Data101/data/flag/")
flag_df <- read_csv("flag.csv")
## New names:
## Rows: 194 Columns: 31
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," chr
## (4): name, mainhue, topleft, botright dbl (27): ...1, landmass, zone, area,
## population, language, religion, bars, ...
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
class(flag_df)
## [1] "spec_tbl_df" "tbl_df" "tbl" "data.frame"
dim(flag_df)
## [1] 194 31
head(flag_df, 5)
## # A tibble: 5 × 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
## # ℹ 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, 5)
## # A tibble: 5 × 31
## ...1 name landmass zone area population language religion bars stripes
## <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 190 Western… 6 3 3 0 1 1 0 0
## 2 191 Yugosla… 3 1 256 22 6 6 0 3
## 3 192 Zaire 4 2 905 28 10 5 0 0
## 4 193 Zambia 4 2 753 6 10 5 3 0
## 5 194 Zimbabwe 4 2 391 8 10 5 0 7
## # ℹ 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>
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
str(flag_df)
## spc_tbl_ [194 × 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>
We are going to use the dplyr package.
flag_df <- as_tibble(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.
flag_df$...1
## [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
What is in this first column? …1
Do we really need it? No.
flag_df <- 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 <- 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.
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
rwb <- flag_df %>%
select(name, red, white, blue) %>%
filter(red == "TRUE", white == "TRUE", blue == "TRUE")
nrow(rwb)
## [1] 63
top10 <- flag_df %>%
select(name, population) %>%
arrange(desc(population))
head(top10, 10)
## # A tibble: 10 × 2
## name population
## <chr> <dbl>
## 1 China 1008
## 2 India 684
## 3 USSR 274
## 4 USA 231
## 5 Indonesia 157
## 6 Brazil 119
## 7 Japan 118
## 8 Bangladesh 90
## 9 Pakistan 84
## 10 Mexico 77
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]
}
landmass <- 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<- 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 <- 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 <-flag_df %>%
group_by(religion) %>%
summarise(
mode_mainhue = cat_mode(mainhue),
median_sunstars = median(sunstars),
num_animate = sum(animate),
perc_animate = mean(animate) * 100
)