-
Notifications
You must be signed in to change notification settings - Fork 68
/
02-Transform-Data.Rmd
237 lines (152 loc) · 4.66 KB
/
02-Transform-Data.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
---
title: "Transform Data"
output: html_notebook
---
```{r setup}
library(tidyverse)
library(babynames)
library(nycflights13)
# Toy datasets 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
)
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"
)
```
## babynames
```{r}
babynames
```
## Your Turn 1
Alter the code to select just the `n` column:
```{r}
select(babynames, name, prop)
```
## Quiz
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 2
Show:
* All of the names where prop is greater than or equal to 0.08
* All of the children named "Sea"
* All of the names that have a missing value for `n`
```{r}
filter(babynames, is.na(n))
```
## Your Turn 3
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 4
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 5
Use `desc()` to find the names with the highest prop.
Then, use `desc()` to find the names with the highest n.
```{r}
```
## Your Turn 6
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}
```
## Exam
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
```{r}
```
## Your Turn 7
Use summarise() 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 8
Extract the rows where `name == "Khaleesi"`. Then use `summarise()` and a summary functions to find:
1. The total number of children named Khaleesi
2. The first year Khaleesi appeared in the data
```{r}
```
## Your Turn 9
Use `group_by()`, `summarise()`, and `arrange()` to display the ten most popular names. Compute popularity as the total number of children of a single gender given a name.
```{r}
```
## Your Turn 10
Use grouping to calculate and then plot the number of children born each year over time.
```{r}
```
## Your Turn 11
Use `min_rank()` and `mutate()` to rank each row in `babynames` from largest `n` to lowest `n`.
```{r}
```
## Your Turn 12
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}
```
## Your Turn 13
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( )
```
## Your Turn 14
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(faa)
```
***
# Take aways
* Extract variables with `select()`
* Extract cases with `filter()`
* Arrange cases, with `arrange()`
* Make tables of summaries with `summarise()`
* 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