-
Notifications
You must be signed in to change notification settings - Fork 13
/
02-Transform.Rmd
341 lines (214 loc) · 6.11 KB
/
02-Transform.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
---
title: "Data Transformation"
output: html_notebook
editor_options:
chunk_output_type: inline
---
<!-- This file by Jake Thompson is licensed under a Creative Commons Attribution 4.0 International License, adapted from the orignal work at https://github.com/rstudio/master-the-tidyverse by RStudio. -->
```{r setup, include = FALSE}
library(tidyverse)
library(babynames)
library(nycflights13)
library(skimr)
```
## Babynames
```{r}
babynames
skim(babynames)
```
## Your Turn 1
Run the `skim_with()` command, and then try skimming babynames again to see how the output is different
```{r}
skim_with(integer = list(p25 = NULL, p75=NULL))
```
## Your Turn 2
Alter the code to select just the `n` column:
```{r}
select(babynames, name, prop)
```
## `select()` helpers
```{r}
select(storms, name:pressure)
select(storms, -c(name, pressure))
select(storms, starts_with("w"))
select(storms, ends_with("e"))
select(storms, contains("d"))
select(storms, matches("^.{4}$"))
select(storms, one_of(c("name", "names", "Name")))
select(storms, num_range("x", 1:5))
```
## Consider
Which of these is NOT a way to select the `name` and `n` columns together?
```{r}
select(babynames, -c(year, sex, prop))
select(babynames, name:n)
select(babynames, starts_with("n"))
select(babynames, ends_with("n"))
```
## Your Turn 3
Show:
* All of the names where prop is greater than or equal to 0.08
* All of the children named "Daenerys"
* All of the names that have a missing value for `n`
```{r}
```
## Your Turn 4
Use Boolean operators to alter the code below to return only the rows that contain:
* Girls named Sea
* Names that were used by exactly 5 or 6 children in 1880
* Names that are one of Acura, Lexus, or Yugo
```{r}
filter(babynames, name == "Sea" | name == "Anemone")
```
## Your Turn 5
Arrange babynames by `n`. Add `prop` as a second (tie breaking) variable to arrange on. Can you tell what the smallest value of `n` is?
```{r}
```
## Your Turn 6
* Use `desc()` to find the names with the highest prop.
* Then, use `desc()` to find the names with the highest n.
```{r}
```
## Steps and the pipe
```{r}
babynames %>%
filter(year == 2015, sex == "M") %>%
select(name, n) %>%
arrange(desc(n))
```
## Your Turn 7
Use `%>%` to write a sequence of functions that:
1. Filter babynames to just the girls that were born in 2015
2. Select the `name` and `n` columns
3. Arrange the results so that the most popular names are near the top.
```{r}
```
## Your Turn 8
1. Trim `babynames` to just the rows that contain your `name` and your `sex`
2. Trim the result to just the columns that will appear in your graph (not strictly necessary, but useful practice)
3. Plot the results as a line graph with `year` on the x axis and `prop` on the y axis, colored by `sex`
```{r}
```
## Your Turn 9
Use `summarize()` to compute three statistics about the data:
1. The first (minimum) year in the dataset
2. The last (maximum) year in the dataset
3. The total number of children represented in the data
```{r}
```
## Your Turn 10
Extract the rows where `name == "Khaleesi"`. Then use `summarize()` to find:
1. The total number of children named Khaleesi
2. The first year Khaleesi appeared in the data
```{r}
```
## Toy data for transforming
```{r}
# Toy dataset to use
pollution <- tribble(
~city, ~size, ~amount,
"New York", "large", 23,
"New York", "small", 14,
"London", "large", 22,
"London", "small", 16,
"Beijing", "large", 121,
"Beijing", "small", 56
)
```
## Summarize
```{r}
pollution %>%
summarize(mean = mean(amount), sum = sum(amount), n = n())
```
```{r}
pollution %>%
group_by(city) %>%
summarize(mean = mean(amount), sum = sum(amount), n = n())
```
## Your Turn 11
Use `group_by()`, `summarize()`, and `arrange()` to display the ten most popular baby names. Compute popularity as the total number of children of a single gender given a name.
```{r}
```
## Your Turn 12
* Use grouping to calculate and then plot the number of children born each year over time.
* Plot the results as a line graph.
```{r}
```
## Mutate
```{r}
babynames %>%
mutate(percent = round(prop * 100, 2))
```
## Your Turn 13
Use `min_rank()` and `mutate()` to rank each row in `babynames` from largest `prop` to lowest `prop`.
```{r}
```
## Your Turn 14
* Compute each name's rank _within its year and sex_.
* Then compute the median rank _for each combination of name and sex_, and arrange the results from highest median rank to lowest.
```{r}
```
## Flights data
```{r}
flights
skim(flights)
```
## Toy data
```{r}
band <- tribble(
~name, ~band,
"Mick", "Stones",
"John", "Beatles",
"Paul", "Beatles"
)
instrument <- tribble(
~name, ~plays,
"John", "guitar",
"Paul", "bass",
"Keith", "guitar"
)
instrument2 <- tribble(
~artist, ~plays,
"John", "guitar",
"Paul", "bass",
"Keith", "guitar"
)
```
## Mutating joins
```{r}
band %>% left_join(instrument, by = "name")
```
## Your Turn 15
Which airlines had the largest arrival delays? Complete the code below.
1. Join `airlines` to `flights`
2. Compute and order the average arrival delays by airline. Display full names, no codes.
```{r}
flights %>%
drop_na(arr_delay) %>%
___ %>%
group_by(___) %>%
___ %>%
arrange(___)
```
## Different names
```{r}
band %>% left_join(instrument2, by = c("name" = "artist"))
```
## Your Turn 16
How many airports in `airports` are serviced by flights originating in New York (i.e. flights in our dataset?) Notice that the column to join on is named `faa` in the **airports** data set and `dest` in the **flights** data set.
```{r}
___ %>%
___(___, by = ___) %>%
distinct(___)
```
***
# Take aways
* Extract variables with `select()`
* Extract cases with `filter()`
* Arrange cases, with `arrange()`
* Make tables of summaries with `summarize()`
* Make new variables, with `mutate()`
* Do groupwise operations with `group_by()`
* Connect operations with `%>%`
* Use `left_join()`, `right_join()`, `full_join()`, or `inner_join()` to join datasets
* Use `semi_join()` or `anti_join()` to filter datasets against each other