-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek7_tutorial.qmd
386 lines (277 loc) · 9.75 KB
/
week7_tutorial.qmd
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
---
title: "ETC1010/5510 Tutorial 7"
subtitle: "Introduction to Data Analysis"
author: "Patrick Li"
date: "Aug 31, 2024"
format:
html:
toc: true
embed-resources: true
---
```{r setup, include = FALSE}
knitr::opts_chunk$set(
echo = TRUE,
eval = FALSE,
message = FALSE,
warning = FALSE,
error = FALSE,
out.width = "70%",
fig.width = 8,
fig.height = 6,
fig.retina = 3)
set.seed(6)
filter <- dplyr::filter
```
## `r emo::ji("target")` Workshop Objectives
- Joining different datasets
- Filtering joined data
- Plotting joined data
## `r emo::ji("wrench")` Instructions
1. In each question, you will replace '___' with your answer. Please note that the Qmd will not knit until you've answered all of the questions.
2. Once you have filled up all the blanks, remember to go to `knitr::opts_chunk` at the top of the document, change `eval = TRUE`, then knit the document.
3. The `emo` package can be installed via `devtools::install_github("hadley/emo")`.
4. This tutorial is rendered using Quarto. Quarto Markdown (QMD) uses the same syntax as R Markdown (RMD), so you can apply your current skills without any changes. While QMD includes advanced features, they are not needed for this unit. As a newer and more versatile document format, QMD has been used in many Business Analytics units within the Department of Econometrics and Business Statistics, especially those in the Master of Business Analytics program. For more information about QMD, you can read [https://quarto.org/docs/faq/rmarkdown.html](https://quarto.org/docs/faq/rmarkdown.html).
## 🚧 Exercise 7A: Understanding joins
#### Data preparation
```{r data}
library(tidyverse)
gap_life <- read_csv("data/gap_life.csv")
gap_life_au <- read_csv("data/gap_life_au.csv")
gap_income <- read_csv("data/gap_income.csv")
gap_income_au <- read_csv("data/gap_income_au.csv")
gap_co2 <- read_csv("data/gap_co2.csv")
gap_co2_au <- read_csv("data/gap_co2_au.csv")
```
### Section A:
We have data on Australian life expectancy and income:
```{r data-view}
gap_life_au
gap_income_au
```
How do we plot life expectancy against income? We need them in the same dataframe!
One technique that we can use is `bind_cols()`, which binds dataframes together, column wise.
```{r bind-data}
bind_cols(gap_life_au,
gap_income_au)
```
But this technique has problems:
1. It produces messy output with repeated columns.
2. It doesn't work unless the dataframes have the same number of rows.
For example, how do we combine the `co2` data with the income or life expectancy data?
```{r data-view2}
gap_co2_au
```
We can't use `bind_cols()`.
```{r data-bind2, eval=FALSE}
bind_cols(gap_co2_au,
gap_income_au)
```
This is because the number of rows are not the same! Thus, we have to use the "joins" to help us.
We can use `left_join()` to combine the income and life expectancy data:
```{r}
left_join(x = gap_income_au,
y = gap_life_au,
by = c("country", "year"))
```
What about adding `co2` data to the income and life expectancy combined data?
We can use another join:
```{r}
left_join(x = gap_income_au,
y = gap_life_au,
by = c("country", "year")) %>%
left_join(gap_co2_au,
by = c("country", "year"))
```
Notice that we get some missing values for `co2`, since we don't have `co2` values for 2015 and beyond.
So, we can combine the data together like so:
```{r}
gap_au <- left_join(x = gap_income_au,
y = gap_life_au,
by = c("country", "year")) %>%
left_join(gap_co2_au,
by = c("country", "year"))
gap_au
```
With the combined data, we can now make a plot!
```{r}
ggplot(gap_au,
aes(x = gdp,
y = life_expectancy)) +
geom_point()
```
### Section B
#### 1. Explain why the following two joins produce different results:
```{r}
left_join(gap_co2_au,
gap_life_au)
left_join(gap_life_au,
gap_co2_au)
```
#### 2. What happens when you add data from New Zealand into the mix? Can you produce a dataset that has all data on gdp, life expectancy, and CO2, for both Australia and New Zealand, using the joining techniques you've just learnt?
```{r}
gap <- left_join(x = gap_income,
y = gap_life,
by = c(___, ___)) %>%
left_join(gap_co2,
by = c(___, ___))
gap
```
## ✈️ Exercise 7B: Mapping data with joins (flights movements)
### Data Preparation
```{r load-plane}
plane_N4YRAA <- read_csv("data/plane_N4YRAA.csv")
glimpse(plane_N4YRAA)
```
Load the airline travel and airport location data.
```{r read-airports}
airport_raw <- read_csv("data/airports.csv")
airport_raw %>%
select(AIRPORT,
LATITUDE,
LONGITUDE,
AIRPORT_STATE_NAME) %>%
glimpse()
```
The key is the airport's three letter code,
- this is used in the ORIGIN and DEST column of the `plane_N4YRAA` table
- and `AIRPORT` is in the `airport` table
First, we will tidy up the `airport_raw` data and create `airport_new`
```{r tidy-airport}
unique(plane_N4YRAA$ORIGIN)
airport_new <- airport_raw %>%
select(AIRPORT,
LATITUDE,
LONGITUDE,
AIRPORT_IS_LATEST,
DISPLAY_AIRPORT_NAME) %>%
filter(AIRPORT_IS_LATEST == 1) %>% # get the rows where airport_is_latest equals 1
select(-AIRPORT_IS_LATEST) # now hide that column because we don't need it anymore and want to clean up the dataset
airport_new
```
`plane_N4YRAA` has fewer airports than `airports`
- We will only keep the rows of the `airport` table, for those that appear in the `plane_N4YRAA` table by doing a left join.
```{r tidy-flight}
N4YRAA_latlon <- left_join(plane_N4YRAA,
airport_new,
by = c("ORIGIN" = "AIRPORT")
) %>%
rename(
"ORIGIN_LATITUDE" = "LATITUDE",
"ORIGIN_LONGITUDE" = "LONGITUDE"
)
N4YRAA_latlon %>%
select(
ORIGIN,
ORIGIN_LATITUDE,
ORIGIN_LONGITUDE,
DISPLAY_AIRPORT_NAME
)
```
The variables `ORIGIN_LATITUDE`, `ORIGIN_LONGITUDE`, `DISPLAY_AIRPORT_NAME` are added to corresponding row in the `plane_N4YRAA` table.
Add destination coordinates
- Added spatial coordinates (latitude, longitude) for the origin airport
- Same needs to be done for the destination airport
Now we combine the current data with some new data to add the destinations.
```{r}
N4YRAA_latlon_dest <- left_join(
N4YRAA_latlon,
airport_new,
by = c("DEST" = "AIRPORT" )
) %>%
rename(
"DEST_LATITUDE" = "LATITUDE",
"DEST_LONGITUDE" = "LONGITUDE"
)
N4YRAA_latlon_dest <- N4YRAA_latlon_dest %>% arrange(FL_DATE, DEP_TIME)
# Cleanup
rm(airport)
```
## 🛫 Exercise 7C: Data wrangling with joins 🛬
### Section A: Data wrangling
Load the nycflights data.
```{r}
library(nycflights13)
```
Run `?airports` to find out more about the `airports` data. Then, look at the lecture notes to figure out the relationship between `flight` data and `airports` data.
The code below filters for the rows with the airport code `'LGA'`
```{r}
airports %>% filter(faa == "LGA")
```
```{r}
delta_a <- flights %>%
filter(carrier == "DL", origin == "LGA", month == 8) %>%
left_join(airports,
by = c("dest" = "faa")) %>%
mutate(orig_lon = -73.9,
orig_lat = 40.8)
```
The code above does the following:
- Step 1: Get flights with the Delta carrier 'DL', that started at the airport code 'LGA' in August
- Step 2: Then join this with `airports` based on the destination code
- Step 3: Create two new columns, with longitude and latitude of the origin airport (LGA)
The code below plots the delay in departure time relating to different wind direction (in degrees) for flights originating from 'LGA', where the wind speed was over 25mph.
```{r}
flgt_weather_a <- flights %>%
filter(origin == "LGA") %>%
left_join(weather, by = c("origin", "time_hour")) %>%
filter(wind_speed > 25)
ggplot(flgt_weather_a,
aes(x = wind_dir,
y = dep_delay)) +
geom_point(alpha=0.5)
```
### Section B: Complete these exercises about the `nycflights13` data using wrangling operations, an appropriate join, and a plot.
##### 1. Filter the flights data to contain just Delta flights ('DL'), for August.
```{r}
delta_b <- flights %>%
___(______)
```
#### 2. Add the airport locations (lat, long) of the _origin_ and _destination_ to the flights data. Adding coordinates would be useful if you were asked to plot the flights on a map, but we won't do this today.
```{r}
flights_latlon <- left_join(delta_b,
airports,
by = c("origin" = "faa")) %>%
rename("origin_latitude" = ___,
"origin_longitude" = ___)
flights_latlon <- left_join(___,
___,
___ = c(___ = ___)) %>%
rename(___ = ___,
___ = ___)
# Or you could do this in one move, by piping the first join into the second:
flights_latlon <- left_join(delta_b,
airports,
by = c("origin" = "faa")
) %>%
rename(
"origin_latitude" = "lat",
"origin_longitude" = "lon"
) %>%
left_join(
airports,
by = c("dest" = "faa" )
) %>%
rename(
"dest_latitude" = "lat",
"dest_longitude" = "lon"
)
```
### Section C. Let's look at how cross winds affect airport operations.
#### 1. Join the `weather` data to the `flights` data
```{r}
flgt_weather_c <- flights %>%
left_join(weather, by = c(___, ___))
```
#### 2. Filter by origin airport 'EWR' and wind speeds higher than 20mph
```{r}
flgt_weath_ewr <- ___ %>%
___(___)
```
#### 3. Plot flight departure delay against wind direction
Does wind direction (when wind speed is stronger) affect operations at the airport?
```{r}
ggplot(___,
aes(x = ___,
y = ___)) +
geom_point(alpha = 0.5)
```