-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchapter_4_excercises.Rmd
362 lines (246 loc) · 6.9 KB
/
chapter_4_excercises.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
---
title: "Chapter 4 excercises"
author: "Hugo Åkerstrand"
date: "`r Sys.Date()`"
output: html_document
editor_options:
markdown:
wrap: 72
---
```{r setup, include=FALSE}
library(tidyverse)
library(nycflights13)
```
### 4.2.5
---
1.
```{r}
flights |>
filter(arr_delay > 120) #Filter out flights that arrive more than two hours delayed
flights |>
filter(dest %in% c("IAH", "HOU")) #Filter out flights with destination Huoston
#Create a vector to filter flights by American, United, Delta
carrier_codes <- airlines |> filter(grepl("American|United|Delta",airlines$name)) |>
select(carrier) |>
unlist(use.names = F)
flights |>
filter(carrier %in% carrier_codes) #Filter out flights by vector
flights |>
filter(month %in% c(7, 8, 9)) #Filter out flights that happened in July, August, & September
flights |>
filter(dep_delay == 0 & arr_delay > 120) #Filter out flights that departed on time, but arrived more than two hours delayed
flights |>
filter(arr_delay >= 60 & (sched_arr_time - sched_dep_time) - (arr_time - dep_time) > 30) #Filter out flights that were delayed by at least an hour, but made up over 30 minutes in flight
```
2.
```{r}
flights |>
arrange(desc(dep_delay), dep_time)
```
3.
```{r}
flights |>
arrange(desc(distance/air_time)) #Sorted on fastest flight
```
4.
```{r}
flights |>
distinct(day,month,year) |> #Filter out each unique date entry to count the number of unique days with flight data
count()
```
5.
```{r}
flights |>
arrange(distance) |> #Top entry is shortest trip
arrange(desc(distance)) #Top entry is longest trip
```
6.
Filtering before arranging results in the minimal computation.
### 4.3.5
1.
The `dep_time` is largely linear with `sched_dep_time`, the `dep_delay` is their delta
```{r}
dep_data <- flights |>
select(dep_time, sched_dep_time, dep_delay)
dep_data
```
2.
```{r}
flights |> glimpse()
```
`dep_time` is the 4th column (type = int), `dep_delay` is the 6th (type = dbl),
`arr_time` is the 7th (type = int), and `arr_delay` is the 9th (type = dbl).
- Most straight forward is to pass the column names or positions, although I
personally prefer names over numbers (for clarity).
- Second most straight forward (and less verbose), would be to use the
`starts_with()` selector.
- More labourously, one could use both positive and negative selection;
first by `select(contains(c("arr_","dep_")))` and then remove the columns
starting with `sched`.
- More complicated options and combinations are of course available,
but this is too complicated to be recommended.
```{r}
flights |>
select(dep_time, dep_delay, arr_time, arr_delay)
```
```{r}
flights |>
select(4, 6, 7, 9)
```
```{r}
flights |>
select(starts_with(c("arr_", "dep_")))
```
```{r}
flights |>
select(
contains(
c("arr_","dep_")) #Include the underscore, or else arr will match to carrier as well
) |>
select(
!contains("sched")
)
```
3.
It produces the same result as if `carrier` was only mentioned once.
```{r}
flights |>
select(carrier, carrier)
```
4.
`any_of()` is a selection helper for a character vector. As opposed to `all_of()`,
this selector is not strict - meaning that it doesn't check to make sure that all
of the vector elements are found.
```{r}
variables <- c("year", "month", "day", "dep_delay", "arr_delay")
flights |> select(any_of(variables))
```
5.
It doesn't surprise me as, select helpers have default setting `.ignore.case = TRUE`.
```{r}
flights |> select(contains("TIME"))
```
6.
```{r}
flights |>
rename(air_time_min = air_time)
```
7.
The error is due to the fact that `select` has removed all columns except
`tailnum`, therefore `arrange` cannot find and sort based on `arr_delay`
### 4.5.7
1.
Let's first look at what carrier has the worst average departure delays (using `dep_delay`)
```{r}
origin_delay <- flights |>
summarise(
n = n(),
delays = mean(dep_delay, na.rm = T), #calculate the average delay
.by = carrier #group by dest
) |>
arrange(desc(delays))
origin_delay |>
ggplot()+
geom_col(aes(x = fct_reorder(carrier, delays), y = delays))
```
However, a lot of the data represent `carrier` and `dest`combinations with few
observations:
```{r}
flights |>
group_by(carrier, dest) |>
summarize(n = n(), .groups = "drop") |>
ggplot()+
geom_histogram(aes(x=n), binwidth = 100)
```
Let's filter the `carrier` and `dest` flights with less than 100 observations,
and then plot the `mean(dep_delay)` for a given `carrier` and `dest`:
```{r}
flights |>
mutate(n = n(),
.before = 1,
.by = c(carrier, dest)) |>
filter(n >= 100) |>
summarize(n = n(),
delays = mean(dep_delay, na.rm = T),
.by = c(carrier,dest)
) |>
ggplot()+
geom_point(aes(x = dest, y = delays, color = carrier),
position = position_dodge(.9))+
facet_wrap( ~ carrier)
origin_delay
```
By looking at these two plots, we can see the difficulty in comparing the average
delay per carrier in the first graph - some carriers only have one specific flight
and become very sensitive to this specific route (e.g. `F9`). At the same time,
comparing DL to EV shows that clearly the latter is having a systematic issue with
delays across all its destinations.
2.
```{r}
flights |>
slice_max(dep_delay, n = 1, by = dest)
```
3.
Delays are much worse in the evening
```{r}
flights |>
summarise(
delays = mean(dep_delay,
na.rm = TRUE),
.by = hour
) |>
ggplot()+
geom_col(aes(x = hour, y = delays))
```
4.
A negative value passed to `n` will make `slice_min()` ignore that many rows of the group - so -1 will make a group of 5 only use 4 rows.
5.
`count()` will calculate the total observations of the group.
If you specify `sort = TRUE` it will put the most numerous observation on the
top of the output.
6.
a. `group_by(y)` will result in two groups belonging either to a or b.
```{r}
df <- tibble(
x = 1:5,
y = c("a", "b", "a", "a", "b"),
z = c("K", "K", "L", "L", "K")
)
df |>
group_by(y)
```
b. It will arrange `df` according to `df$y` (a, then b) - the df is not grouped!
```{r}
df |>
arrange(y)
```
c. It will give give a 2x2 tibble: a `y` column (a or b) and `mean_x` column of x
```{r}
df |>
group_by(y) |>
summarize(mean_x = mean(x))
```
d. Same as above, but a third column `z` and another row as a consequence of
`group_by(y, z)`. The message states that the output is now grouped by only `y`,
rather than also `z`.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
```
e. It is different in not maintaining any grouping variable.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x), .groups = "drop")
```
f. They are different in `mutate` adding a column, and `summarize` producing a
new one, consisting of grouping variables and the `mean_x`.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
df |>
group_by(y, z) |>
mutate(mean_x = mean(x))
```