-
Notifications
You must be signed in to change notification settings - Fork 10
/
lab_tidyverse.Rmd
328 lines (241 loc) · 7.59 KB
/
lab_tidyverse.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
---
title: "Tidy Work in Tidyverse"
subtitle: "R Programming Foundation for Life Scientists"
output:
bookdown::html_document2:
highlight: textmate
toc: true
toc_float:
collapsed: true
smooth_scroll: true
print: false
toc_depth: 4
number_sections: true
df_print: default
code_folding: none
self_contained: false
keep_md: false
encoding: 'UTF-8'
css: "assets/lab.css"
include:
after_body: assets/footer-lab.html
---
```{r,child="assets/header-lab.Rmd"}
```
```{r,echo=FALSE,message=FALSE,warning=FALSE,results='hide'}
library(dplyr)
library(nycflights13)
```
# Introduction
Welcome to the hands-on workshop "Tidy Work in Tidyverse". Most of the things necessary to complete the tutorials and challenges were covered in the lecture. However, sometimes the tasks require that you check the docs or search online. Not all our solutions are optimal. Let us know if you can do better or solve things in a different way. If stuck, look at hints, next google and if still stuck, turn to TA. It is a lot of material, do not fee bad if you do not solve all tasks. Good luck!
# Pipes
- Rewrite the following code chunk as one pipe (`magrittr`):
```{r,eval=FALSE}
my_cars <- mtcars[, c(1:4, 7)]
my_cars <- my_cars[my_cars$disp > mean(my_cars$disp), ]
print(my_cars)
my_cars <- colMeans(my_cars)
```
```{r, accordion=TRUE, echo=TRUE, eval=FALSE}
my_cars <- mtcars %>%
select(c(1:4, 7)) %>%
filter(disp > mean(disp)) %T>%
print() %>%
colMeans()
```
- Rewrite the correlations below using pipes.
```{r,eval=FALSE}
cor(mtcars)
```
```{r, accordion=TRUE, echo=TRUE, eval=FALSE}
mtcars %>% cor()
```
```{r,eval=FALSE}
cor(mtcars$gear, mtcars$mpg)
```
```{r, accordion=TRUE, echo=TRUE, eval=FALSE}
mtcars %$% cor(gear, mpg)
```
# Tibbles
- Convert the `mtcars` dataset to a tibble `vehicles`.
```{r,accordion=TRUE}
vehicles <- mtcars %>% as_tibble()
```
- Select the number of cylinders (`cyl`) variable using:
- the `[[index]]` accessor,
- the `[[string]]` accessor,
- the `$` accessor.
```{r,accordion=TRUE}
vehicles[['cyl']]
vehicles[[2]]
vehicles$cyl
```
- Do the same selection as above, but using pipe and placeholders (use all three ways of accessing a variable).
```{r,accordion=TRUE}
vehicles %T>%
{print(.[['cyl']])} %T>%
{print(.[[2]])} %>%
.$cyl
```
- Print the tibble.
```{r,accordion=TRUE}
vehicles
```
- Print the 30 first rows of the tibble.
```{r,accordion=TRUE}
vehicles %>% head(n = 30)
```
- Change the default behaviour of printing a tibble so that at least 15 and at most 30 rows are printed.
```{r,accordion=TRUE}
options(tibble.print_min = 15, tibble.print_max = 30)
```
- Convert `vehicles` back to a `data.frame` called `automobiles`.
```{r,accordion=TRUE}
automobiles <- as.data.frame(vehicles)
```
Do you think tibbles are lazy? Try to create a tibble that tests whether *lazy evaluation* applies to tibbles too.
```{r eval=FALSE}
tibble(x = sample(1:10, size = 10, replace = T), y = log10(x))
```
# NYC flights Challenge
The `nycflights13` package contains information about all flights that departed from NYC (i.e., EWR, JFK and LGA) in 2013: 336,776 flights with 16 variables. To help understand what causes delays, it also includes a number of other useful datasets: weather, planes, airports, airlines. We will use it to train working with tibbles and `dplyr`.
## Selecting columns
- Load the `nycflights13` package (install if necessary)
```{r,accordion=TRUE,eval=FALSE}
install.packages('nycflights13')
library(nycflights13)
```
- Read about the data in the package docs
```{r,accordion=TRUE}
?nycflights13
```
- Inspect the `flights` tibble.
```{r,accordion=TRUE}
flights
```
- Select all columns but `carrier` and `arr_time`
```{r,accordion=TRUE}
flights %>% select(-carrier, -arr_time)
```
- Select `carrier`, `tailnum` and `origin`
```{r,accordion=TRUE}
flights %>% select(carrier, tailnum, origin)
```
- Hide columns from `day` through `carrier`
```{r,accordion=TRUE}
flights %>% select(-(day:carrier))
```
- Select all columns that have to do with `arr`_ival (hint: `?tidyselect`)
```{r,accordion=TRUE}
flights %>% select(contains('arr_'))
```
- Select columns based on a vector `v <- c("arr_time", "sched_arr_time", "arr_delay")`
```{r,accordion=TRUE}
v <- c("arr_time", "sched_arr_time", "arr_delay")
flights %>% select(v) # or
flights %>% select(one_of(v))
```
- Rename column `dest` to `destination` using `select()` and `rename()`. What is the difference between the two approaches?
```{r,accordion=TRUE}
flights %>% select(destination = dest) %>% head()
flights %>% rename(destination = dest) %>% head()
# select keeps only the renamed column while rename returns the whole dataset
# with the column renamed
```
## Filtering rows
- Filter only the flights that arrived ahead of schedule
```{r,accordion=TRUE}
flights %>% filter(arr_delay < 0)
```
- Filter the flights that had departure delay between 10 and 33
```{r,accordion=TRUE}
flights %>% filter(dep_delay >= 10, dep_delay <= 33) # or
flights %>% filter(between(dep_delay, 10, 33))
```
- Fish out all flights with unknown arrival time
```{r,accordion=TRUE}
flights %>% filter(is.na(arr_time))
```
- Retrieve rows 1234:1258 (hint: `?slice`)
```{r,accordion=TRUE}
flights %>% slice(1234:1258)
```
- Sample (`?sample_n()`) 3 random flights per day in March
```{r,accordion=TRUE}
flights %>% filter(month == 3) %>%
group_by(day) %>%
sample_n(3)
```
- Show 5 most departure-delayed flights in January per carrier
```{r,accordion=TRUE}
flights %>%
filter(month == 1) %>%
group_by(carrier) %>%
top_n(5, dep_delay)
```
- How many unique routes exists?
```{r,accordion=TRUE}
flights %>%
mutate(route=paste(origin,"-",dest)) %>%
distinct(route,.keep_all=T) %>%
nrow()
```
- Which is the most frequent route?
```{r,accordion=TRUE}
# JFK - LAX
flights %>%
mutate(route=paste(origin,"-",dest)) %>%
group_by(route) %>%
count() %>%
arrange(-n)
```
## Trans(mutations)
- `air_time` is the amount of time in minutes spent in the air. Add a new column `air_spd` that will contain aircraft's airspeed in mph
```{r,accordion=TRUE}
flights %>% mutate(air_spd = distance/(air_time / 60))
```
- As above, but keep only the new `air_spd` variable
```{r,accordion=TRUE}
flights %>% transmute(air_spd = distance/(air_time / 60))
```
## Groups and counts
- Use `group_by()`, `summarise()` and `n()` to see how many planes were delayed (departure) every month
```{r,accordion=TRUE}
flights %>%
filter(dep_delay > 0) %>%
group_by(month) %>%
summarise(num_dep_delayed = n())
```
- What was the mean `dep_delay` per month?
```{r,accordion=TRUE}
flights %>%
group_by(month) %>%
summarise(mean_dep_delay = mean(dep_delay, na.rm = T))
```
- Count the number of incoming delayed flights from each unique origin and sort origins by this count (descending)
```{r,accordion=TRUE}
flights %>%
filter(arr_delay > 0) %>%
group_by(origin) %>%
summarise(cnt = n()) %>%
arrange(desc(cnt))
```
- Use `summarise()` to sum total `dep_delay` per month in hours
```{r,accordion=TRUE}
flights %>%
group_by(month) %>%
summarize(tot_dep_delay = sum(dep_delay/60, na.rm = T))
```
- Run `group_size()` on `carrier` what does it return?
```{r,accordion=TRUE}
flights %>%
group_by(carrier) %>%
group_size()
```
- Use `n_groups()` to check the number of unique origin-carrier pairs,
```{r,accordion=TRUE}
flights %>%
group_by(carrier) %>%
n_groups()
```
**Note on `ungroup`** Depending on the version of `dplyr` you may or may need to use the `ungroup()` if you want to group your data on some other variables. In the newer versions, `summarise` and `mutate` drop one aggregation level.