forked from mine-cetinkaya-rundel/r4ds-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-transform.qmd
291 lines (213 loc) · 6.54 KB
/
data-transform.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
---
title: "Data transformation"
---
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
```
## Prerequisites {.unnumbered}
```{r}
library(nycflights13)
library(tidyverse)
```
## 4.2.5 Exercises {.unnumbered}
1. Pipelines for each part are given below.
a.
```{r}
flights |>
filter(arr_delay >= 120) |>
arrange(desc(arr_delay))
```
b.
```{r}
flights |>
filter(dest %in% c("IAH", "HOU"))
```
c.
```{r}
flights |>
filter(carrier %in% c("UA", "DL", "AA"))
```
d.
```{r}
flights |>
filter(month %in% c(7, 8, 9))
```
e.
```{r}
flights |>
filter(arr_delay >= 120 & dep_delay <= 0) |> view()
```
f.
```{r}
flights |>
filter(dep_delay >= 60 & dep_delay - arr_delay > 30)
```
2. Flights with longest departure delays and, among those, flights that left earliest in the morning:
```{r}
flights |>
arrange(desc(dep_delay)) |>
arrange(sched_dep_time) |>
relocate(dep_delay, sched_dep_time)
```
3. Fastest flights, measured as miles per hour:
```{r}
flights |>
mutate(speed = distance / (air_time / 60)) |>
arrange(desc(speed)) |>
relocate(speed)
```
4. Yes, there was a flight on every day of 2013 since there are 365 distinct combinations of `year`, `month`, and `day`, which is equal to the number of days in the year 2013.
```{r}
flights |>
distinct(year, month, day) |>
nrow()
```
5. Flights that traveled the farthest distance:
```{r}
flights |>
arrange(desc(distance)) |>
relocate(distance)
```
Flights that traveled the shortest distance
```{r}
flights |>
arrange(distance) |>
relocate(distance)
```
6. The order doesn't matter because we filter based on a condition, not based on row number.
## 4.3.5 Exercises {.unnumbered}
1. I would expect `dep_time` to be `sched_dep_time + dep_delay`.
```{r}
flights |>
relocate(dep_time, sched_dep_time, dep_delay)
```
2. The following are some of the ways these variables can be selected.
```{r}
flights |>
select(dep_time, dep_delay, arr_time, arr_delay)
flights |>
select(starts_with("dep"), starts_with("arr"))
flights |>
select(dep_time:arr_delay, -contains("sched"))
```
3. You get the variable just once.
```{r}
flights |>
select(dep_time, dep_time)
```
4. You ask if `any_of()` these variables have a certain thing you are looking for.
```{r}
variables <- c("year", "month", "day", "dep_delay", "arr_delay")
flights |>
select(any_of(variables))
```
5. Yes, it does surprise me since the variable names are lowercase but the string in `contains()` is uppercase. By default, `contains()` ignores case.
```{r}
flights |>
select(contains("TIME"))
```
To change this default behavior, set `ignore.case = FALSE`.
```{r}
flights |>
select(contains("TIME", ignore.case = FALSE))
```
6. Below we rename `air_time` to `air_time_min` and move it to the beginning of the data frame.
```{r}
flights |>
rename(air_time_min = air_time) |>
relocate(air_time_min)
```
7. This doesn't work because the result of the `select()` step is a data frame with only the `tailnum` variable, so it's not possible to arrange it by another variable, `arr_delay`.
```{r}
#| error: true
flights |>
select(tailnum) |>
arrange(arr_delay)
```
## 4.5.7 Exercises {.unnumbered}
1. F9 (Frontier Airlines) has the worst average delays.
```{r}
flights |>
group_by(carrier) |>
summarize(avg_dep_delay = mean(dep_delay, na.rm = TRUE)) |>
arrange(desc(avg_dep_delay))
```
2. The following are the top 5 most departure delayed flights from each destination.
```{r}
flights |>
group_by(dest) |>
arrange(dest, desc(dep_delay)) |>
slice_head(n = 5) |>
relocate(dest, dep_delay)
```
3. Over the course of the day, hourly average departure delay increases until about 7pm, and then declines again, however doesn't go as low as the beginning of the day.
```{r}
flights |>
group_by(hour) |>
summarize(avg_dep_delay = mean(dep_delay, na.rm = TRUE)) |>
ggplot(aes(x = hour, y = avg_dep_delay)) +
geom_smooth()
```
4. Supplying a negative value arranges the data frame in either ascending (with `slice_min()`) or descending (with `slice_max()`) order, but it doesn't actually slice the data frame for the lowest/highest values of the given variable.
```{r}
flights |>
slice_min(dep_delay, n = -5) |>
relocate(dep_delay)
flights |>
slice_min(dep_delay, n = 5) |>
relocate(dep_delay)
flights |>
slice_max(dep_delay, n = -5) |>
relocate(dep_delay)
flights |>
slice_max(dep_delay, n = 5) |>
relocate(dep_delay)
```
5. `count()` counts the number of observations in each group, setting the `sort` argument to `TRUE` arranges the categories in descending order of number of observations.
6. First, let's define the data frame `df`.
```{r}
df <- tibble(
x = 1:5,
y = c("a", "b", "a", "a", "b"),
z = c("K", "K", "L", "L", "K")
)
```
a. The following groups `df` by `y`.
```{r}
df |>
group_by(y)
```
b. The following arranges `df` in ascending order of the value of `y`.
```{r}
df |>
arrange(y)
```
c. The following groups `df` by `y` and then calculates the average value of `x` for each group.
```{r}
df |>
group_by(y) |>
summarize(mean_x = mean(x))
```
d. The following groups `df` by `y` and `z`, and then calculates the average value of `x` for each group combination. The resulting data frame is grouped by `y`.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
```
e. The following groups `df` by `y` and `z`, and then calculates the average value of `x` for each group combination. The resulting data frame is not grouped.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x), .groups = "drop")
```
f. Each of the following groups `df` by `y` and `z`, and then calculates the average value of `x` for each group combination. With `summarize()` the resulting data frame has one row per group combination while with `mutate()` the resulting data frame has the same number of rows as the original data frame.
```{r}
df |>
group_by(y, z) |>
summarize(mean_x = mean(x))
df |>
group_by(y, z) |>
mutate(mean_x = mean(x))
```