-
Notifications
You must be signed in to change notification settings - Fork 0
/
wednesday.Rmd
322 lines (254 loc) · 7.03 KB
/
wednesday.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
---
title: "Wdnesday_Chapter4"
author: "Luca Mannino"
date: "21/09/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(nycflights13)
library(tidyverse)
```
```{r}
library(tidyverse)
library(nycflights13)
by_dest <- group_by(flights, dest)
delay <- summarise(by_dest,
count = n(),
dist = mean(distance, na.rm = TRUE),
delay = mean(arr_delay, na.rm = TRUE)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
delay <- filter(delay, count > 20, dest != "HNL")# It looks like delays increase with distance up to ~750 miles
# and then decrease. Maybe as flights get longer there's more
# ability to make up delays in the air?
ggplot(data = delay, mapping = aes(x = dist, y = delay)) +
geom_point(aes(size = count), alpha = 1/3) +
geom_smooth(se = FALSE)
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'count(flights, dest)
flights %>%
group_by(dest) %>%
summarise(n = n())
class(flights)
daily <- group_by(flights, year, month, day)
class(daily)
(per_day <- summarise(daily, flights = n(), .groups = "keep"))
flights_rowwise <- flights %>%
rowwise()
microbenchmark::microbenchmark(
mutate(flights_rowwise, arr_time2 = arr_time + 1),
mutate(flights, arr_time2 = arr_time + 1),
times = 5)
mutate(flights_rowwise, arr_time2 = arr_time + 1) %>%
ungroup()
```
```{r}
not_cancelled <- flights %>%
filter(!is.na(dep_delay), !is.na(arr_delay))
not_cancelled %>%
group_by(year, month, day) %>%
summarise(mean = mean(dep_delay))
```
delays <- not_cancelled %>%
group_by(tailnum) %>%
summarise(
delay = mean(arr_delay)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
ggplot(data = delays, mapping = aes(x = delay)) +
geom_freqpoly(binwidth = 10)
```{r}
delays <- not_cancelled %>%
group_by(tailnum) %>%
summarise(
delay = mean(arr_delay)
)
#> `summarise()` ungrouping output (override with `.groups` argument)
ggplot(data = delays, mapping = aes(x = delay)) +
geom_freqpoly(binwidth = 10)
```
Brainstorm at least 5 different ways to assess the typical delay characteristics of a group of flights. Consider the following scenarios:
A flight is 15 minutes early 50% of the time, and 15 minutes late 50% of the time.
A flight is always 10 minutes late.
A flight is 30 minutes early 50% of the time, and 30 minutes late 50% of the time.
99% of the time a flight is on time. 1% of the time it’s 2 hours late.
```{r}
class(flights)
?flights
flights %>%
group_by(flight) %>%
summarise(med_delay = median(arr_delay, na.rm=TRUE)) %>%
filter(med_delay <= -15)
flights %>%
group_by(flight) %>%
summarise(prop_delay = mean(arr_delay, na.rm=TRUE)) %>%
filter(prop_delay <= -15)
```
```{r}
flights %>%
group_by(flight) %>%
summarise(prop_very_early = mean(arr_delay <= -15, na.rm = TRUE),
n = n()) %>%
filter(prop_very_early >= 0.5, n >= 20) %>%
arrange(desc(prop_very_early))
```
```{r}
flights %>%
group_by(flight) %>%
summarise(prop_very_early = mean(arr_delay <= -15, na.rm = TRUE),
n = n()) %>%
filter(prop_very_early == 1) %>%
arrange(desc(n))
```
not_cancelled %>% count(dest) and not_cancelled %>% count(tailnum, wt = distance) (without using count())
```{r}
not_cancelled %>% count(dest)
```
wt = weighted sum
```{r}
not_cancelled %>%
group_by(dest) %>%
summarise(destination = unique(dest)#, na.rm = TRUE),
,n = n())
not_cancelled %>%
group_by(dest) %>%
summarise(n = n())
```
```{r}
not_cancelled %>% count(tailnum, wt = distance)
```
```{r}
not_cancelled %>%
group_by(tailnum) %>%
summarise(destination = unique(dest)#, na.rm = TRUE),
,n = n())
```
```{r}
table(dep = is.na(flights$dep_delay), ar = is.na(flights$arr_delay))
```
```{r}
not_cancelled2 <- filter(flights, !is.na(arr_delay))
microbenchmark::microbenchmark(
any(is.na(not_cancelled2)),
anyNA(not_cancelled2),
times = 10)
```
```{r}
mean(complete.cases(not_cancelled2))
```
```{r}
table(dep = is.na(flights$dep_delay),
arr = is.na(flights$arr_delay))
not_cancelled2 <- filter(flights, !is.na(arr_delay))
microbenchmark::microbenchmark(
any(is.na(not_cancelled2)),
anyNA(not_cancelled2),
times = 10
)
mean(complete.cases(not_cancelled2))
flights %>%
filter(., complete.cases(.))
f(x, y)
y %>% f(x, .)
```
```{r}
flights %>%
group_by(year, month, day) %>%
summarise(nb_canceled = sum(is.na(dep_delay)),
n = n()) %>%
arrange(desc(nb_canceled))
```
```{r}
flights %>%
group_by(year, month, day) %>%
summarize(prop_canceled = mean(is.na(dep_delay)),
avg_delay = mean(dep_delay, na.rm = TRUE)) %>%
ggplot(aes(prop_canceled, avg_delay)) +
geom_point() +
theme_bw(13) +
geom_smooth()
```
sum depends on group operators don't
which plane has the worst on time regord
```{r}
flights %>%
group_by(tailnum) %>%
filter(sum(!is.na(arr_delay))>0) %>%
summarise(max_arr_delay = max(arr_delay, na.rm = TRUE)) %>%
arrange(tailnum)
```
```{r}
flights %>%
group_by(tailnum) %>%
filter(sum(!is.na(arr_delay)) > 0) %>%
summarise(max_delay = max(arr_delay, na.rm = TRUE)) %>%
arrange(desc(max_delay)) %>%
slice_max(max_delay, n = 1)
```
What time of day should you fly if you want to avoid delays as much as possible?
```{r}
flights %>%
group_by(hour) %>%
summarise(avg_delay = mean(arr_delay, na.rm=TRUE, n =n() %>%
ggplot()))
```
```{r}
```
mutate join
```{r}
flights2 <- flights %>%
select(year:day, hour, origin, dest, tailnum, carrier)
flights2
```
airlines is anothe table
```{r}
flights2 %>%
select(-origin, -dest) %>%
left_join(airlines, by = "carrier")
```
Compute the average delay by destination, then join on the airports data frame so you can show the spatial distribution of delays. Here’s an easy way to draw a map of the United States:
```{r}
#should have added na.rm= true as shown in next snip
flights2 <- flights %>%
group_by(dest) %>%
filter(arr_delay>0) %>%
summarise(avg_arr_delay = mean(arr_delay))# %>%
#filter(avg_arr_delay > 0)
flights2
airports %>%
left_join(flights2, c("faa" = "dest"))
airports %>%
semi_join(flights, c("faa" = "dest")) %>%
left_join(flights2,c("faa" = "dest"))%>%
ggplot(aes(lon, lat)) +
borders("state") +
geom_point(aes(size=avg_arr_delay)) +
#scale_color_viridis_c()+
coord_quickmap()
```
remmber na.rm = true
```{r}
avg_delay_per_dest <- flights %>%
group_by(dest) %>%
summarize(avg_delay = mean(arr_delay, na.rm = TRUE)) %>%
print()
avg_delay_per_dest %>%
left_join(airports, by = c("dest" = "faa")) %>%
ggplot(aes(lon, lat, size = avg_delay, color = avg_delay)) +
borders("state") +
geom_point() +
coord_quickmap() +
theme_bw(13) +
scale_color_viridis_c(direction = -1)
```
```{r}
flights %>%
left_join(select(airports, c(faa, lat, lon)),
by = c("dest" = "faa")) %>%
left_join(select(airports, c(faa, lat, lon)),
by = c("origin" = "faa"), suffix = c("_dest", "_origin"))
```
```{r}
```