forked from batpigandme/tidynomicon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
practice.Rmd
598 lines (420 loc) · 16.1 KB
/
practice.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# Practice
```{r setup, include=FALSE}
source("etc/common.R")
```
We have covered a lot in the last few lessons,
so this one presents some practice exercises to ground what we have learned
and introduce a few more commonly-used functions.
## Working with a single tidy table
### Load the tidyverse collection of libraries and the `here` library for constructing paths:
```{r fake-load-libraries, eval=FALSE}
library(tidyverse)
library(here)
```
### Use `here::here` to construct a path to a file and `readr::read_csv` to read that file:
```{r read-survey-data}
path = here::here("data", "person.csv")
person <- readr::read_csv(path)
person
```
*Read `survey/site.csv`.*
### Count rows and columns using `nrow` and `ncol`:
```{r count-rows-and-columns}
nrow(person)
ncol(person)
```
*How many rows and columns are in the site data?*
### Format strings using `glue::glue`:
```{r use-glue}
print(glue::glue("person has {nrow(person)} rows and {ncol(person)} columns"))
```
*Print a nicely-formatted summary of the number of rows and columns in the site data.*
### Use `colnames` to get the names of columns and `paste` to join strings together:
```{r use-colnames-and-paste}
print(glue::glue("person columns are {paste(colnames(person), collapse = ' ')}"))
```
*Print a nicely-formatted summary of the names of the columns in the site data.*
### Use `dplyr::select` to create a new table with a subset of columns by name:
```{r select-by-name}
dplyr::select(person, family_name, personal_name)
```
*Create a table with just the latitudes and longitudes of sites.*
### Use `dplyr::filter` to create a new table with a subset of rows by values:
```{r filter-with-one-condition}
dplyr::filter(person, family_name < "M")
```
*Create a table with only sites south of -48 degrees.*
### Use the pipe operator `%>%` to combine operations:
```{r select-pipe-filter}
person %>%
dplyr::select(family_name, personal_name) %>%
dplyr::filter(family_name < "M")
```
*Create a table with only the latitudes and longitudes of sites south of -48 degrees.*
### Use `dplyr::mutate` to create a new column with calculated values and `stringr::str_length` to calculate string length:
```{r mutate-name-length}
person %>%
dplyr::mutate(name_length = stringr::str_length(family_name))
```
*Use the built-in function `round` to create a table with latitudes and longitudes rounded to integers.*
### Use `dplyr::arrange` to order rows and (optionally) `dplyr::desc` to impose descending order:
```{r mutate-and-arrange}
person %>%
dplyr::mutate(name_length = stringr::str_length(family_name)) %>%
dplyr::arrange(dplyr::desc(name_length))
```
*Create a table sorted by decreasing longitude (i.e., most negative longitude last).*
## Working with grouped data
### Read `survey/measurements.csv` and look at the data with `View`:
```{r read-and-view}
measurements <- readr::read_csv(here::here("data", "measurements.csv"))
View(measurements)
```
### Find rows where `reading` is not NA and saved as `cleaned`:
```{r remove-reading-na}
cleaned <- measurements %>%
dplyr::filter(!is.na(reading))
cleaned
```
*Rewrite the filter expression to select rows where the visitor and quantity are not NA either.*
```{r hidden-cleaned, echo=FALSE}
cleaned <- measurements %>%
dplyr::filter(!is.na(visitor) & !is.na(quantity) & !is.na(reading))
```
### Group measurements by quantity measured and count the number of each (the column is named `n` automatically):
```{r group-by-quantity}
cleaned %>%
dplyr::group_by(quantity) %>%
dplyr::count()
```
*Group by person and quantity measured.*
### Find the minimum, average, and maximum for each quantity:
```{r min-ave-max}
cleaned %>%
dplyr::group_by(quantity) %>%
dplyr::summarize(low = min(reading), mid = mean(reading), high = max(reading))
```
*Look at the range for each combination of person and quantity.*
### Rescale salinity measurements that are greater than 1:
```{r rescale-salinity}
cleaned <- cleaned %>%
dplyr::mutate(reading = ifelse(quantity == 'sal' & reading > 1.0, reading/100, reading))
cleaned
```
*Do the same calculation use `case_when`.*
### Read `visited.csv`, drop the NAs, and join with the cleaned-up table of readings:
```{r join-two-tables}
cleaned <- readr::read_csv(here::here("data", "visited.csv")) %>%
dplyr::filter(!is.na(visit_date)) %>%
dplyr::inner_join(cleaned, by = c("visit_id" = "visit_id"))
cleaned
```
*Join `visited.csv` with `site.csv` to get (date, latitude, longitude) triples for site visits.*
### Find the dates of the highest radiation reading at each site:
```{r dates-high-rad}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(max_rad = max(reading)) %>%
dplyr::filter(reading == max_rad)
```
Another way to do it:
```{r dates-high-rad-2}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::top_n(1, reading) %>%
dplyr::select(site_id, visit_date, reading)
```
*Explain why this __doesn't__ work.*
```{r error-high-rad, error=TRUE}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::summarize(max_rad = max(reading)) %>%
dplyr::ungroup() %>%
dplyr::filter(reading == max_rad)
```
### Normalize radiation against the highest radiation seen per site:
```{r normalize-rad}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(
max_rad = max(reading),
frac_rad = reading / max_rad) %>%
dplyr::select(visit_id, site_id, visit_date, frac_rad)
```
*Normalize salinity against mean salinity by site.*
### Find stepwise change in radiation per site by date:
```{r rad-change}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(delta_rad = reading - dplyr::lag(reading)) %>%
dplyr::arrange(site_id, visit_date)
```
*Find length of time between visits by site.*
### Find sites that experience any stepwise increase in radiation between visits:
```{r rad-increases}
cleaned %>%
dplyr::filter(quantity == "rad") %>%
dplyr::group_by(site_id) %>%
dplyr::mutate(delta_rad = reading - dplyr::lag(reading)) %>%
dplyr::filter(!is.na(delta_rad)) %>%
dplyr::summarize(any_increase = any(delta_rad > 0)) %>%
dplyr::filter(any_increase)
```
*Find sites with visits more than one year apart.*
## Creating charts
We will use data on the mass and home range area (HRA) of various species from:
> Tamburello N, Côté IM, Dulvy NK (2015) Data from: Energy and the scaling of animal space use. Dryad Digital Repository.
> https://doi.org/10.5061/dryad.q5j65
```{r}
hra <- readr::read_csv(here::here("data", "home-range-database.csv"))
head(hra)
```
### Look at how mass is distributed:
```{r chart-mass}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = mean.mass.g))
```
Try again with `log10.mass`:
```{r chart-log-mass}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = log10.mass))
```
*Create histograms showing the distribution of home range area using linear and log scales.*
### Change the visual appearance of a chart:
```{r change-visual}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = log10.mass), bins = 100) +
ggplot2::ggtitle("Frequency of Species Masses") + ggplot2::xlab("Log10 of Mass") + ggplot2::ylab("Number of Species") +
ggplot2::theme_minimal()
```
*Show the distribution of home range areas with a dark background.*
### Create a scatterplot showing the relationship between mass and home range area:
```{r scatterplot}
ggplot2::ggplot(hra) +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra))
```
*Create a similar scatterplot showing the relationship between the raw values rather than the log values.*
### Colorize scatterplot points by class:
```{r colorize-scatterplot}
hra %>%
dplyr::mutate(class_fct = as.factor(class)) %>%
ggplot2::ggplot() +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra, color = class_fct), alpha = 0.5)
```
**Group by order and experiment with different alpha values.*
### Create a faceted plot:
```{r facet-plot}
hra %>%
dplyr::mutate(class_fct = as.factor(class)) %>%
ggplot2::ggplot() +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra, color = class_fct), alpha = 0.5) +
ggplot2::facet_wrap(~class_fct)
```
*Create a plot faceted by order for just the reptiles.*
### Fit a linear regression to the logarithmic data for birds:
```{r fit-line}
hra %>%
dplyr::filter(class == "aves") %>%
ggplot2::ggplot() +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra), alpha = 0.5) +
ggplot2::geom_smooth(method = lm, mapping = aes(x = log10.mass, y = log10.hra), color = 'red')
```
*Fit a line to the raw data for birds rather than the logarithmic data.*
### Create a violin plot of mass by order for birds:
```{r violin-plot}
hra %>%
dplyr::filter(class == "aves") %>%
dplyr::mutate(order_fct = as.factor(order)) %>%
ggplot2::ggplot() +
ggplot2::geom_violin(mapping = aes(x = order_fct, y = log10.mass, color = order_fct))
```
*Rotate the labels on the X axis to make this readable, then explain the gaps.*
### Display the same data as a boxplot:
```{r box-plot}
hra %>%
dplyr::filter(class == "aves") %>%
dplyr::mutate(order_fct = as.factor(order)) %>%
ggplot2::ggplot() +
ggplot2::geom_boxplot(mapping = aes(x = order_fct, y = log10.mass, color = order_fct))
```
*Fix the labels and remove orders that only contain one species.*
### Save the linear regression plot for birds as a PNG:
```{r save-file}
hra %>%
dplyr::filter(class == "aves") %>%
ggplot2::ggplot() +
ggplot2::geom_point(mapping = aes(x = log10.mass, y = log10.hra), alpha = 0.5) +
ggplot2::geom_smooth(method = lm, mapping = aes(x = log10.mass, y = log10.hra), color = 'red')
ggsave("/tmp/birds.png")
```
*Save the plot as SVG scaled to be 8cm wide.*
### Create a horizontal histogram with 50 bins:
```{r horizontal}
ggplot2::ggplot(hra) +
ggplot2::geom_histogram(mapping = aes(x = log10.mass), bins = 50) +
ggplot2::coord_flip()
```
*Use `stat_summary` to summarize the relationship between mass and home range area by class.*
## Writing functions
### Write and call a function that returns one column from a file:
```{r define-function, message=FALSE}
col_from_file <- function(filename, colname) {
dat <- readr::read_csv(filename)
dat[colname]
}
person_filename <- here::here("data", "person.csv")
col_from_file(person_filename, "family_name")
```
*Write a function that reads a file and changes the name of one column.*
### Define a default value for a parameter:
```{r default-value, message=FALSE}
col_from_file <- function(filename, colname, na = c("", "NA")) {
dat <- readr::read_csv(filename, na = na)
dat[colname]
}
col_from_file(person_filename, "family_name", c("Dyer"))
```
*Write a function that only keeps the first 100 rows of a table unless another value is passed.*
### Name the value passed for a parameter to make intention clearer:
```{r named-passing}
col_from_file(person_filename, "family_name", na = c("Dyer"))
```
*Call your function with all of the parameters out of order.*
### Make functions slightly more robust:
```{r with-error-check, message=FALSE, error=TRUE}
col_from_file <- function(filename, colname, na = c("", "NA")) {
dat <- readr::read_csv(filename, na = na)
stopifnot(colname %in% colnames(dat))
dat[colname]
}
col_from_file(person_filename, "FAMILY", na = c("Dyer"))
```
*Fail if the number of rows asked for is less than or equal to zero.*
### Use quoting and splicing to avoid needing a string for the column name:
```{r nse-single, message=FALSE}
cols_from_file <- function(filename, colname, na = c("", "NA")) {
colname = rlang::enquo(colname)
readr::read_csv(filename, na = na) %>%
dplyr::select(!!colname)
}
cols_from_file(person_filename, personal_name)
```
*Modify your function so that users can pass `num_rows/2` or a similar expression to specify the number of rows they want to keep.*
### Select any number of columns:
```{r quote-multi-column, message=FALSE}
cols_from_file <- function(filename, ..., na = c("", "NA")) {
readr::read_csv(filename, na = na) %>%
dplyr::select(...)
}
cols_from_file(person_filename, personal_name, family_name)
```
*Can you call `cols_from_file` to subtract columns rather than keeping them?*
### Add an optional filter condition:
```{r}
cols_from_file <- function(filename, ..., na = c("", "NA"), filter_by = NULL) {
temp <- read_csv(filename, na = na) %>%
dplyr::select(...)
filter_by <- rlang::enquo(filter_by)
if (!is.null(filter_by)) {
temp <- temp %>%
filter(!!filter_by)
}
temp
}
cols_from_file(person_filename, personal_name, family_name, filter_by = family_name > "M")
```
*Why doesn't this work if the call to `rlang::enquo` is moved inside the conditional?*
### Run a function safely:
```{r nonexistent-column, message=FALSE, error=TRUE}
cols_from_file(person_filename, NONEXISTENT)
```
```{r run-safely-fail, message=FALSE}
safe_cols_from_file <- purrr::safely(cols_from_file)
safe_cols_from_file(person_filename, NONEXISTENT)
```
```{r run-safely-succeed, message=FALSE}
safe_cols_from_file(person_filename, person_id)
```
*When is `purrr::safely` safe to use?*
### Unpack multiple values in a single assignment:
```{r unpack, message=FALSE}
library(zeallot)
c(result, error) %<-% safe_cols_from_file(person_filename, person_id)
result
error
```
*What happens if there are more variables on the left of `%<-%` than there are values on the right?*
### Assign out of function scope with `<<-`:
```{r assign-upward, message=FALSE}
call_count = 0
safe_cols_from_file <- purrr::safely(function(filename, ..., na = c("", "NA")) {
call_count <<- call_count + 1
readr::read_csv(filename, na = na) %>%
dplyr::select(...)
})
c(result, error) %<-% safe_cols_from_file(person_filename, family_name)
call_count
```
*What happens if the variable assigned to with `<<-` does not exist before the assignment?*
## Functional programming
### Apply a function to every element of a vector:
```{r purrr-map}
long_name <- function(name){
stringr::str_length(name) > 4
}
purrr::map(person$family_name, long_name)
```
*Modify this example to return a logical vector.*
### Use an anonymous function:
```{r anonymous-function}
purrr::map_lgl(person$family_name, function(name) stringr::str_length(name) > 4)
```
*Create a character vector with all names in upper case.*
### Use a formula with `.x` as a shorthand for an anonymous function of one argument:
```{r}
purrr::map_chr(person$family_name, ~ stringr::str_to_upper(.x))
```
*Do you actually need `purrr::map_chr` to do this?*
### Map a function of two arguments:
```{r}
purrr::map2_chr(person$personal_name, person$family_name, ~stringr::str_c(.y, .x, sep = '_'))
```
*Calculate a vector of `mean.mass.g` over `mean.hra.m2` for the `hra` table using `purrr::map2_dbl`, then explain how you ought to do it instead.*
### Map a function of three or more arguments:
```{r}
vals = list(first = person$personal_name, last = person$family_name, ident = person$person_id)
purrr::pmap_chr(vals, function(ident, last, first) stringr::str_c(first, last, ident, sep = '_'))
```
*Repeat this without giving names to the elements of `vals`.*
### Flattern one level of a nested structure:
```{r}
purrr::flatten(list(person$personal_name, person$family_name))
```
*Use `purrr::keep` to discard the elements from the flattened list that are greater than "M".*
### Check whether every element passes a test:
```{r}
purrr::every(person$personal_name, ~.x > 'M')
```
*Use `some` to check whether any of the elements pass the same test.*
### Modify specific elements of a list:
```{r}
purrr::modify_at(person$personal_name, c(2, 4), stringr::str_to_upper)
```
*Use `modify_if` to upper-case names that are greater than "M".*
### Create an acronym:
```{r}
purrr::reduce(person$personal_name, ~stringr::str_c(.x, stringr::str_sub(.y, 1, 1)), .init = "")
```
*Explain why using `stringr::str_c(stringr::str_sub(.x, 1, 1), stringr::str_sub(.y, 1, 1))` doesn't work.*
### Create intermediate values:
```{r}
purrr::accumulate(person$personal_name, ~stringr::str_c(.x, stringr::str_sub(.y, 1, 1)), .init = "")
```
*Modify this so that the initial empty string isn't in the final result.*
```{r links, child="etc/links.md"}
```