-
Notifications
You must be signed in to change notification settings - Fork 0
/
cm012-exercise.Rmd
269 lines (206 loc) · 8.07 KB
/
cm012-exercise.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
---
title: "cm012 Exercises: Factors"
output:
html_document:
# keep_md: true
theme: paper
---
```{r allow errors, echo = FALSE}
knitr::opts_chunk$set(error = TRUE)
```
```{r load, echo = FALSE, message = FALSE, warning = FALSE}
library(gapminder)
library(tidyverse)
library(dplyr)
library(forcats)
library(ggplot2)
library(DT)
```
## Motivating the need for factors in R
### Activity 1: Using Factors for plotting
**1.1** Let's look again into `gapminder` dataset and create a new cloumn, `life_level`, that contains five categories ("very high", "high","moderate", "low" and "very low") based on life expectancy in 1997. Assign categories accoring to the table below:
| Criteria | life_level|
|-------------|-----------|
| less than 23 | very low |
| between 23 and 48 | low |
| between 48 and 59 | moderate |
| between 59 and 70 | high |
| more than 70 | very high |
Function `case_when()` is a tidier way to vectorise multiple `if_else()` statements. you can read more about this function [here](https://dplyr.tidyverse.org/reference/case_when.html).
```{r}
# kind of like a switch/case statement in other langauges
gapminder %>%
filter(year == 1997) %>%
mutate(life_level = case_when(lifeExp < 23 ~ "very low",
lifeExp < 48 ~ "low",
lifeExp < 59 ~ "moderate",
lifeExp < 70 ~ "high",
TRUE ~ "very high")) %>%
ggplot() + geom_boxplot(aes(x = life_level, y = gdpPercap)) +
labs(y = "GDP per capita, $", x= "Life expectancy level, years") +
theme_bw()
```
Do you notice anything odd/wrong about the graph?
We can make a few observations:
- It seems that none of the countries had a "very low" life-expectancy in 1997.
- However, since it was an option in our analysis it should be included in our plot. Right?
- Notice also how levels on x-axis are placed in the "wrong" order.
**1.2** You can correct these issues by explicitly setting the levels parameter in the call to `factor()`. Use, `drop = FALSE` to tell the plot not to drop unused levels
```{r}
# can use ylab and xlab instead of labs as well
gapminder %>%
filter(year == 1997) %>%
mutate(life_level = factor(case_when(lifeExp < 23 ~ "very low",
lifeExp < 48 ~ "low",
lifeExp < 59 ~ "moderate",
lifeExp < 70 ~ "high",
TRUE ~ "very high") ,
levels = c("very low", "low", "moderate", "high", "very high")
)
) %>%
ggplot() + geom_boxplot(aes(x = life_level, y = gdpPercap)) +
labs(y = "GDP per capita, $", x= "Life expectancy level, years") +
scale_x_discrete(drop = FALSE) +
theme_bw()
```
## Inspecting factors (activity 2)
In Activity 1, we created our own factors, so now let's explore what categorical variables that we have in the `gapminder` dataset.
### Exploring `gapminder$continent` (activity 2.1)
Use functions such as `str()`, `levels()`, `nlevels()` and `class()` to answer the following questions:
- what class is `continent`(a factor or charecter)?
- How many levels? What are they?
- What integer is used to represent factor "Asia"?
```{r}
class(gapminder$continent)
levels(gapminder$continent)
nlevels(gapminder$continent)
str(gapminder$continent)
# first few continents are Asia-- that's why str() yields 3,3,3
# because Asia is 3rd in levels()
datatable(gapminder)
```
### Exploring `gapminder$country` (activity 2.2)
Let's explore what else we can do with factors:
Answer the following questions:
- How many levels are there in `country`?
- Filter `gapminder` dataset by 5 countries of your choice. How many levels are in your filtered dataset?
```{r}
nlevels(gapminder$country)
# use %in% for 'membership checks', like "in" in python
countries <- c("Canada", "United States", "Mexico", "China", "Japan")
gap <- gapminder %>%
filter(country %in% countries)
# Factor levels preserved?
nlevels(gap$country)
```
## Dropping unused levels
What if we want to get rid of some levels that are "unused" - how do we do that?
The function `droplevels()` operates on all the factors in a data frame or on a single factor. The function `forcats::fct_drop()` operates on a factor.
```{r}
h_gap_dropped <- gap %>%
droplevels()
# these functions must have 'factors' as input, NOT a tibble
nlevels(h_gap_dropped$country)
```
## Changing the order of levels
Let's say we wanted to re-order the levels of a factor using a new metric - say, count().
We should first produce a frequency table as a tibble using `dplyr::count()`:
```{r}
gapminder %>%
count(continent)
```
The table is nice, but it would be better to visualize the data.
Factors are most useful/helpful when plotting data.
So let's first plot this:
```{r}
# ylab is actually the x-axis after the coord_flip()
# xlab is actually the y-axis after the coord_flip()
gapminder %>%
ggplot() +
geom_bar(aes(continent)) +
coord_flip() +
theme_bw() +
ylab("Number of entries") +
xlab("Continent")
```
Think about how levels are normally ordered.
It turns out that by default, R always sorts levels in alphabetical order.
However, it is preferable to order the levels according to some principle:
1. Frequency/count.
- Make the most common level the first and so on. Function `fct_infreq()` might be useful.
- The function `fct_rev()` will sort them in the opposite order.
For instance ,
`
```{r}
gapminder %>%
ggplot() +
geom_bar(aes(fct_rev(fct_infreq(continent)))) +
coord_flip()+
theme_bw() +
ylab("Number of entries") +
xlab("Continent")
```
Section 9.6 of Jenny Bryan's [notes](https://stat545.com/factors-boss.html#reorder-factors) has some helpful examples.
2. Another variable.
- For example, if we wanted to bring back our example of ordering `gapminder` countries by life expectancy, we can visualize the results using `fct_reorder()`.
```{r}
# default summarizing function is median() NOT mean()
# ordered by life Expectancy, but is not evident as the bars are measuring number of entries
gapminder %>%
ggplot() +
geom_bar(aes(fct_reorder(continent, lifeExp, max))) +
coord_flip()+
theme_bw() +
xlab("Continent") +
ylab("Number of entries")
```
Use `fct_reorder2()` when you have a line chart of a quantitative x against another quantitative y and your factor provides the color.
```{r}
## order by life expectancy
ggplot(h_gap, aes(x = year, y = lifeExp,
color = fct_reorder2(country,year,lifeExp))) +
geom_line() +
labs(color = "country")
```
## Change order of the levels manually
This might be useful if you are preparing a report for say, the state of affairs in Africa.
```{r}
# can also specify levels directly, using levels = c() in fct_relevel()
gapminder %>%
ggplot() +
geom_bar(aes(fct_relevel(continent,"Oceania", after = 2))) +
coord_flip()+
theme_bw() +
xlab("Continent") + ylab("Number of entries")
```
More details on reordering factor levels by hand can be found [here] https://forcats.tidyverse.org/reference/fct_relevel.html
### Recoding factors
Sometimes you want to specify what the levels of a factor should be.
For instance, if you had levels called "blk" and "brwn", you would rather they be called "Black" and "Brown" - this is called recoding.
Lets recode `Oceania` and the `Americas` in the graph above as abbreviations `OCN` and `AME` respectively using the function `fct_recode()`.
```{r}
# similar to rename()
gapminder %>%
ggplot() +
geom_bar(aes(fct_recode(continent,"OCN"="Oceania", "AME" = "Americas"))) +
coord_flip()+
theme_bw() +
xlab("Continent") + ylab("Number of entries")
```
<!--
## Grow a factor (OPTIONAL)
Let’s create two data frames,`df1` and `df2` each with data from two countries, dropping unused factor levels.
```{r}
```
The country factors in df1 and df2 have different levels.
Can we just combine them?
```{r}
```
The country factors in `df1` and `df2` have different levels.
Can you just combine them using `c()`?
```{r}
```
Explore how different forms of row binding work behave here, in terms of the country variable in the result.
```{r}
```
-->