forked from jrnold/r4ds-exercise-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.Rmd
857 lines (671 loc) · 23.5 KB
/
functions.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# Functions {#functions .r4ds-section}
## Introduction {#introduction-12 .r4ds-section}
```{r message=FALSE,cache=FALSE}
library("tidyverse")
library("lubridate")
```
## When should you write a function? {#when-should-you-write-a-function .r4ds-section}
### Exercise 19.2.1 {.unnumbered .exercise data-number="19.2.1"}
<div class="question">
Why is `TRUE` not a parameter to `rescale01()`?
What would happen if `x` contained a single missing value, and `na.rm` was `FALSE`?
</div>
<div class="answer">
The code for `rescale01()` is reproduced below.
```{r}
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
```
If `x` contains a single missing value and `na.rm = FALSE`, then this function stills return a non-missing value.
```{r}
rescale01_alt <- function(x, na.rm = FALSE) {
rng <- range(x, na.rm = na.rm, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
rescale01_alt(c(NA, 1:5), na.rm = FALSE)
rescale01_alt(c(NA, 1:5), na.rm = TRUE)
```
The option `finite = TRUE` to `range()` will drop all non-finite elements, and `NA` is a non-finite element.
However, if both `finite = FALSE` and `na.rm = FALSE`, then this function will return a vector of `NA` values.
Recall, arithmetic operations involving `NA` values return `NA`.
```{r}
rescale01_alt2 <- function(x, na.rm = FALSE, finite = FALSE) {
rng <- range(x, na.rm = na.rm, finite = finite)
(x - rng[1]) / (rng[2] - rng[1])
}
rescale01_alt2(c(NA, 1:5), na.rm = FALSE, finite = FALSE)
```
</div>
### Exercise 19.2.2 {.unnumbered .exercise data-number="19.2.2"}
<div class="question">
In the second variant of `rescale01()`, infinite values are left unchanged.
Rewrite `rescale01()` so that `-Inf` is mapped to `0`, and `Inf` is mapped to `1`.
</div>
<div class="answer">
```{r}
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE)
y <- (x - rng[1]) / (rng[2] - rng[1])
y[y == -Inf] <- 0
y[y == Inf] <- 1
y
}
rescale01(c(Inf, -Inf, 0:5, NA))
```
</div>
### Exercise 19.2.3 {.unnumbered .exercise data-number="19.2.3"}
<div class="question">
Practice turning the following code snippets into functions. Think about what each function does. What would you call it? How many arguments does it need? Can you rewrite it to be more expressive or less duplicative?
```{r eval=FALSE}
mean(is.na(x))
x / sum(x, na.rm = TRUE)
sd(x, na.rm = TRUE) / mean(x, na.rm = TRUE)
```
</div>
<div class="answer">
This code calculates the proportion of `NA` values in a vector.
```{r eval=FALSE}
mean(is.na(x))
```
I will write it as a function named `prop_na()` that takes a single argument `x`,
and returns a single numeric value between 0 and 1.
```{r}
prop_na <- function(x) {
mean(is.na(x))
}
prop_na(c(0, 1, 2, NA, 4, NA))
```
This code standardizes a vector so that it sums to one.
```{r eval=FALSE}
x / sum(x, na.rm = TRUE)
```
I'll write a function named `sum_to_one()`, which is a function of a single argument, `x`, the vector to standardize, and an optional argument `na.rm`.
The optional argument, `na.rm`, makes the function more expressive, since it can
handle `NA` values in two ways (returning `NA` or dropping them).
Additionally, this makes `sum_to_one()` consistent with `sum()`, `mean()`, and many
other R functions which have a `na.rm` argument.
While the example code had `na.rm = TRUE`, I set `na.rm = FALSE` by default
in order to make the function behave the same as the built-in functions like `sum()` and `mean()` in its handling of missing values.
```{r}
sum_to_one <- function(x, na.rm = FALSE) {
x / sum(x, na.rm = na.rm)
}
```
```{r}
# no missing values
sum_to_one(1:5)
# if any missing, return all missing
sum_to_one(c(1:5, NA))
# drop missing values when standardizing
sum_to_one(c(1:5, NA), na.rm = TRUE)
```
This code calculates the [coefficient of variation](https://en.wikipedia.org/wiki/Coefficient_of_variation) (assuming that `x` can only take non-negative values), which is the standard deviation divided by the mean.
```{r eval=FALSE}
sd(x, na.rm = TRUE) / mean(x, na.rm = TRUE)
```
I'll write a function named `coef_variation()`, which takes a single argument `x`,
and an optional `na.rm` argument.
```{r}
coef_variation <- function(x, na.rm = FALSE) {
sd(x, na.rm = na.rm) / mean(x, na.rm = na.rm)
}
coef_variation(1:5)
coef_variation(c(1:5, NA))
coef_variation(c(1:5, NA), na.rm = TRUE)
```
</div>
### Exercise 19.2.4 {.unnumbered .exercise data-number="19.2.4"}
<div class="question">
Follow <https://nicercode.github.io/intro/writing-functions.html> to write your own functions to compute the variance and skew of a numeric vector.
</div>
<div class="answer">
**Note** The math in <https://nicercode.github.io/intro/writing-functions.html> seems not to be rendering.
The sample variance is defined as,
$$
\mathrm{Var}(x) = \frac{1}{n - 1} \sum_{i=1}^n (x_i - \bar{x}) ^2 \text{,}
$$
where $\bar{x} = (\sum_i^n x_i) / n$ is the sample mean.
The corresponding function is:
```{r variance}
variance <- function(x, na.rm = TRUE) {
n <- length(x)
m <- mean(x, na.rm = TRUE)
sq_err <- (x - m)^2
sum(sq_err) / (n - 1)
}
```
```{r}
var(1:10)
variance(1:10)
```
There are multiple definitions for [skewness](https://en.wikipedia.org/wiki/Skewness), but we will use the following one,
$$
\mathrm{Skew}(x) = \frac{\frac{1}{n - 2}\left(\sum_{i=1}^{n}(x_{i} - \bar x)^3\right)}{\mathrm{Var}(x)^{3 / 2}} \text{.}
$$
The corresponding function is:
```{r skewness}
skewness <- function(x, na.rm = FALSE) {
n <- length(x)
m <- mean(x, na.rm = na.rm)
v <- var(x, na.rm = na.rm)
(sum((x - m) ^ 3) / (n - 2)) / v ^ (3 / 2)
}
```
```{r}
skewness(c(1, 2, 5, 100))
```
</div>
### Exercise 19.2.5 {.unnumbered .exercise data-number="19.2.5"}
<div class="question">
Write `both_na()`, a function that takes two vectors of the same length and returns the number of positions that have an `NA` in both vectors.
</div>
<div class="answer">
```{r}
both_na <- function(x, y) {
sum(is.na(x) & is.na(y))
}
both_na(
c(NA, NA, 1, 2),
c(NA, 1, NA, 2)
)
both_na(
c(NA, NA, 1, 2, NA, NA, 1),
c(NA, 1, NA, 2, NA, NA, 1)
)
```
</div>
### Exercise 19.2.6 {.unnumbered .exercise data-number="19.2.6"}
<div class="question">
What do the following functions do? Why are they useful even though they are so short?
</div>
<div class="answer">
```{r}
is_directory <- function(x) file.info(x)$isdir
is_readable <- function(x) file.access(x, 4) == 0
```
The function `is_directory()` checks whether the path in `x` is a directory.
The function `is_readable()` checks whether the path in `x` is readable, meaning that the file exists and the user has permission to open it.
These functions are useful even though they are short because their names make it much clearer what the code is doing.
</div>
### Exercise 19.2.7 {.unnumbered .exercise data-number="19.2.7"}
<div class="question">
Read the complete lyrics to ``Little Bunny Foo Foo''. There’s a lot of duplication in this song. Extend the initial piping example to recreate the complete song, and use functions to reduce the duplication.
</div>
<div class="answer">
The lyrics of one of the [most common versions](https://en.wikipedia.org/wiki/Little_Bunny_Foo_Foo) of this song are
> Little bunny Foo Foo \
> Hopping through the forest \
> Scooping up the field mice \
> And bopping them on the head
>
> Down came the Good Fairy, and she said \
> "Little bunny Foo Foo \
> I don't want to see you \
> Scooping up the field mice
>
> And bopping them on the head. \
> I'll give you three chances, \
> And if you don't stop, I'll turn you into a GOON!" \
> And the next day...
The verses repeat with one chance fewer each time.
When there are no chances left, the Good Fairy says
> "I gave you three chances, and you didn't stop; so...." \
> POOF. She turned him into a GOON! \
> And the moral of this story is: *hare today, goon tomorrow.*
Here's one way of writing this
```{r eval=FALSE}
threat <- function(chances) {
give_chances(
from = Good_Fairy,
to = foo_foo,
number = chances,
condition = "Don't behave",
consequence = turn_into_goon
)
}
lyric <- function() {
foo_foo %>%
hop(through = forest) %>%
scoop(up = field_mouse) %>%
bop(on = head)
down_came(Good_Fairy)
said(
Good_Fairy,
c(
"Little bunny Foo Foo",
"I don't want to see you",
"Scooping up the field mice",
"And bopping them on the head."
)
)
}
lyric()
threat(3)
lyric()
threat(2)
lyric()
threat(1)
lyric()
turn_into_goon(Good_Fairy, foo_foo)
```
</div>
## Functions are for humans and computers {#functions-are-for-humans-and-computers .r4ds-section}
### Exercise 19.3.1 {.unnumbered .exercise data-number="19.3.1"}
<div class="question">
Read the source code for each of the following three functions, puzzle out what they do, and then brainstorm better names.
```{r}
f1 <- function(string, prefix) {
substr(string, 1, nchar(prefix)) == prefix
}
f2 <- function(x) {
if (length(x) <= 1) return(NULL)
x[-length(x)]
}
f3 <- function(x, y) {
rep(y, length.out = length(x))
}
```
</div>
<div class="answer">
The function `f1` tests whether each element of the character vector `nchar`
starts with the string `prefix`. For example,
```{r}
f1(c("abc", "abcde", "ad"), "ab")
```
A better name for `f1` is `has_prefix()`
The function `f2` drops the last element of the vector `x`.
```{r}
f2(1:3)
f2(1:2)
f2(1)
```
A better name for `f2` is `drop_last()`.
The function `f3` repeats `y` once for each element of `x`.
```{r}
f3(1:3, 4)
```
Good names would include `recycle()` (R's name for this behavior) or `expand()`.
</div>
### Exercise 19.3.2 {.unnumbered .exercise data-number="19.3.2"}
<div class="question">
Take a function that you’ve written recently and spend 5 minutes brainstorming a better name for it and its arguments.
</div>
<div class="answer">
Answer left to the reader.
</div>
### Exercise 19.3.3 {.unnumbered .exercise data-number="19.3.3"}
<div class="question">
Compare and contrast `rnorm()` and `MASS::mvrnorm()`. How could you make them more consistent?
</div>
<div class="answer">
`rnorm()` samples from the univariate normal distribution, while `MASS::mvrnorm`
samples from the multivariate normal distribution. The main arguments in
`rnorm()` are `n`, `mean`, `sd`. The main arguments is `MASS::mvrnorm` are `n`,
`mu`, `Sigma`. To be consistent they should have the same names. However, this
is difficult. In general, it is better to be consistent with more widely used
functions, e.g. `rmvnorm()` should follow the conventions of `rnorm()`. However,
while `mean` is correct in the multivariate case, `sd` does not make sense in
the multivariate case. However, both functions are internally consistent.
It would not be good practice to have `mu` and `sd` as arguments or `mean` and `Sigma` as arguments.
</div>
### Exercise 19.3.4 {.unnumbered .exercise data-number="19.3.4"}
<div class="question">
Make a case for why `norm_r()`, `norm_d()` etc would be better than `rnorm()`, `dnorm()`. Make a case for the opposite.
</div>
<div class="answer">
If named `norm_r()` and `norm_d()`, the naming convention groups functions by their
distribution.
If named `rnorm()`, and `dnorm()`, the naming convention groups functions
by the action they perform.
- `r*` functions always sample from distributions: for example,
`rnorm()`, `rbinom()`, `runif()`, and `rexp()`.
- `d*` functions calculate the probability density or mass of a distribution:
For example, `dnorm()`, `dbinom()`, `dunif()`, and `dexp()`.
R distributions use this latter naming convention.
</div>
## Conditional execution {#conditional-execution .r4ds-section}
### Exercise 19.4.1 {.unnumbered .exercise data-number="19.4.1"}
<div class="question">
What’s the difference between `if` and `ifelse()`? > Carefully read the help and construct three examples that illustrate the key differences.
</div>
<div class="answer">
The keyword `if` tests a single condition, while `ifelse()` tests each element.
</div>
### Exercise 19.4.2 {.unnumbered .exercise data-number="19.4.2"}
<div class="question">
Write a greeting function that says “good morning”, “good afternoon”, or “good evening”, depending on the time of day. (Hint: use a time argument that defaults to `lubridate::now()`. That will make it easier to test your function.)
</div>
<div class="answer">
```{r}
greet <- function(time = lubridate::now()) {
hr <- lubridate::hour(time)
# I don't know what to do about times after midnight,
# are they evening or morning?
if (hr < 12) {
print("good morning")
} else if (hr < 17) {
print("good afternoon")
} else {
print("good evening")
}
}
greet()
greet(ymd_h("2017-01-08:05"))
greet(ymd_h("2017-01-08:13"))
greet(ymd_h("2017-01-08:20"))
```
</div>
### Exercise 19.4.3 {.unnumbered .exercise data-number="19.4.3"}
<div class="question">
Implement a `fizzbuzz()` function. It takes a single number as input. If the
number is divisible by three, it returns “fizz”. If it’s divisible by five it
returns “buzz”. If it’s divisible by three and five, it returns “fizzbuzz”.
Otherwise, it returns the number. Make sure you first write working code before
you create the function.
</div>
<div class="answer">
We can use modulo operator, `%%`, to check divisibility.
The expression `x %% y` returns 0 if `y` divides `x`.
```{r}
1:10 %% 3 == 0
```
A more concise way of checking for divisibility is to note that the not operator will return `TRUE` for 0, and `FALSE` for all non-zero numbers.
Thus, `!(x %% y)`, will check whether `y` divides `x`.
```{r}
!(1:10 %% 3)
```
There are four cases to consider:
1. If `x` is divisible by 3 and 5, then return "fizzbuzz".
1. If `x` is divisible by 3 and not 5, then return "fizz".
1. If `x` is divisible by 5 and not 3, then return "buzz".
1. Otherwise, which is the case in which `x` is not divisible by either 3 or 5, return `x`.
The key to answering this question correctly, is to first check whether `x`
is divisible by both 3 and 5.
If the function checks whether `x` is divisible by 3 or 5 before considering the case that the number is divisible by both, then the function will never return `"fizzbuzz"`.
```{r}
fizzbuzz <- function(x) {
# these two lines check that x is a valid input
stopifnot(length(x) == 1)
stopifnot(is.numeric(x))
if (!(x %% 3) && !(x %% 5)) {
"fizzbuzz"
} else if (!(x %% 3)) {
"fizz"
} else if (!(x %% 5)) {
"buzz"
} else {
# ensure that the function returns a character vector
as.character(x)
}
}
fizzbuzz(6)
fizzbuzz(10)
fizzbuzz(15)
fizzbuzz(2)
```
This function can be slightly improved by combining the first two lines conditions so
we only check whether `x` is divisible by 3 once.
```{r}
fizzbuzz2 <- function(x) {
# these two lines check that x is a valid input
stopifnot(length(x) == 1)
stopifnot(is.numeric(x))
if (!(x %% 3)) {
if (!(x %% 5)) {
"fizzbuzz"
} else {
"fizz"
}
} else if (!(x %% 5)) {
"buzz"
} else {
# ensure that the function returns a character vector
as.character(x)
}
}
fizzbuzz2(6)
fizzbuzz2(10)
fizzbuzz2(15)
fizzbuzz2(2)
```
Instead of only accepting one number as an input, we could a FizzBuzz function that works on a vector.
The `case_when()` function vectorizes multiple if-else conditions, so is perfect for this task.
In fact, fizz-buzz is used in the examples in the documentation of `case_when()`.
```{r}
fizzbuzz_vec <- function(x) {
case_when(!(x %% 3) & !(x %% 5) ~ "fizzbuzz",
!(x %% 3) ~ "fizz",
!(x %% 5) ~ "buzz",
TRUE ~ as.character(x)
)
}
fizzbuzz_vec(c(0, 1, 2, 3, 5, 9, 10, 12, 15))
```
The following function is an example of a vectorized FizzBuzz function that
only uses bracket assignment.
```{r}
fizzbuzz_vec2 <- function(x) {
y <- as.character(x)
# put the individual cases first - any elements divisible by both 3 and 5
# will be overwritten with fizzbuzz later
y[!(x %% 3)] <- "fizz"
y[!(x %% 3)] <- "buzz"
y[!(x %% 3) & !(x %% 5)] <- "fizzbuzz"
y
}
fizzbuzz_vec2(c(0, 1, 2, 3, 5, 9, 10, 12, 15))
```
This question, called the ["Fizz Buzz"](https://en.wikipedia.org/wiki/Fizz_buzz) question, is a common programming interview question used for screening out programmers who can't program.[^fizzbuzz]
</div>
### Exercise 19.4.4 {.unnumbered .exercise data-number="19.4.4"}
<div class="question">
How could you use `cut()` to simplify this set of nested if-else statements?
</div>
<div class="answer">
```{r eval=FALSE}
if (temp <= 0) {
"freezing"
} else if (temp <= 10) {
"cold"
} else if (temp <= 20) {
"cool"
} else if (temp <= 30) {
"warm"
} else {
"hot"
}
```
How would you change the call to `cut()` if I’d used `<` instead of `<=`? What is the other chief advantage of cut() for this problem? (Hint: what happens if you have many values in temp?)
```{r}
temp <- seq(-10, 50, by = 5)
cut(temp, c(-Inf, 0, 10, 20, 30, Inf),
right = TRUE,
labels = c("freezing", "cold", "cool", "warm", "hot")
)
```
To have intervals open on the left (using `<`), I change the argument to `right = FALSE`,
```{r}
temp <- seq(-10, 50, by = 5)
cut(temp, c(-Inf, 0, 10, 20, 30, Inf),
right = FALSE,
labels = c("freezing", "cold", "cool", "warm", "hot")
)
```
Two advantages of using `cut` is that it works on vectors, whereas `if` only works on a single value (I already demonstrated this above),
and that to change comparisons I only needed to change the argument to `right`, but I would have had to change four operators in the `if` expression.
</div>
### Exercise 19.4.5 {.unnumbered .exercise data-number="19.4.5"}
<div class="question">
What happens if you use `switch()` with numeric values?
</div>
<div class="answer">
In `switch(n, ...)`, if `n` is numeric, it will return the `n`th argument from `...`.
This means that if `n = 1`, `switch()` will return the first argument in `...`,
if `n = 2`, the second, and so on.
For example,
```{r}
switch(1, "apple", "banana", "cantaloupe")
switch(2, "apple", "banana", "cantaloupe")
```
If you use a non-integer number for the first argument of `switch()`, it will
ignore the non-integer part.
```{r}
switch(1.2, "apple", "banana", "cantaloupe")
switch(2.8, "apple", "banana", "cantaloupe")
```
Note that `switch()` truncates the numeric value, it does not round to the nearest integer.
While it is possible to use non-integer numbers with `switch()`, you should avoid it
</div>
### Exercise 19.4.6 {.unnumbered .exercise data-number="19.4.6"}
<div class="question">
What does this `switch()` call do? What happens if `x` is `"e"`?
```{r eval=FALSE}
x <- "e"
switch(x,
a = ,
b = "ab",
c = ,
d = "cd"
)
```
Experiment, then carefully read the documentation.
</div>
<div class="answer">
First, let's write a function `switcheroo()`, and see what it returns for different values of `x`.
```{r}
switcheroo <- function(x) {
switch(x,
a = ,
b = "ab",
c = ,
d = "cd"
)
}
switcheroo("a")
switcheroo("b")
switcheroo("c")
switcheroo("d")
switcheroo("e")
switcheroo("f")
```
The `switcheroo()` function returns `"ab"` for `x = "a"` or `x = "b"`,
`"cd"` for `x = "c"` or `x = "d"`, and
`NULL` for `x = "e"` or any other value of `x` not in `c("a", "b", "c", "d")`.
How does this work?
The `switch()` function returns the first non-missing argument value for the first name it matches.
Thus, when `switch()` encounters an argument with a missing value, like `a = ,`,
it will return the value of the next argument with a non missing value, which in this case is `b = "ab"`.
If `object` in `switch(object=)` is not equal to the names of any of its arguments,
`switch()` will return either the last (unnamed) argument if one is present or `NULL`.
Since `"e"` is not one of the named arguments in `switch()` (`a`, `b`, `c`, `d`),
and no other unnamed default value is present, this code will return `NULL`.
The code in the question is shorter way of writing the following.
```{r eval=FALSE}
switch(x,
a = "ab",
b = "ab",
c = "cd",
d = "cd",
NULL # value to return if x not matched
)
```
</div>
## Function arguments {#function-arguments .r4ds-section}
### Exercise 19.5.1 {.unnumbered .exercise data-number="19.5.1"}
<div class="question">
What does `commas(letters, collapse = "-")` do? Why?
</div>
<div class="answer">
The `commas()` function in the chapter is defined as
```{r}
commas <- function(...) {
str_c(..., collapse = ", ")
}
```
When `commas()` is given a collapse argument, it throws an error.
```{r error=TRUE}
commas(letters, collapse = "-")
```
This is because when the argument `collapse` is given to `commas()`, it
is passed to `str_c()` as part of `...`.
In other words, the previous code is equivalent to
```{r eval=FALSE}
str_c(letters, collapse = "-", collapse = ", ")
```
However, it is an error to give the same named argument to a function twice.
One way to allow the user to override the separator in `commas()` is to add a `collapse`
argument to the function.
```{r}
commas <- function(..., collapse = ", ") {
str_c(..., collapse = collapse)
}
```
</div>
### Exercise 19.5.2 {.unnumbered .exercise data-number="19.5.2"}
<div class="question">
It’d be nice if you could supply multiple characters to the `pad` argument, e.g. `rule("Title", pad = "-+")`.
Why doesn’t this currently work? How could you fix it?
</div>
<div class="answer">
This is the definition of the rule function from the [chapter](https://r4ds.had.co.nz/functions.html).
```{r}
rule <- function(..., pad = "-") {
title <- paste0(...)
width <- getOption("width") - nchar(title) - 5
cat(title, " ", str_dup(pad, width), "\n", sep = "")
}
```
```{r}
rule("Important output")
```
You can currently supply multiple characters to the `pad` argument, but the output will not be the desired width.
The `rule()` function duplicates `pad` a number of times
equal to the desired width minus the length of the title and five extra characters.
This implicitly assumes that `pad` is only one character. If `pad` were two character,
the output will be almost twice as long.
```{r}
rule("Valuable output", pad = "-+")
```
One way to handle this is to use `str_trunc()` to truncate the string,
and `str_length()` to calculate the number of characters in the `pad` argument.
```{r}
rule <- function(..., pad = "-") {
title <- paste0(...)
width <- getOption("width") - nchar(title) - 5
padding <- str_dup(
pad,
ceiling(width / str_length(title))
) %>%
str_trunc(width)
cat(title, " ", padding, "\n", sep = "")
}
rule("Important output")
rule("Valuable output", pad = "-+")
rule("Vital output", pad = "-+-")
```
Note that in the second output, there is only a single `-` at the end.
</div>
### Exercise 19.5.3 {.unnumbered .exercise data-number="19.5.3"}
<div class="question">
What does the `trim` argument to `mean()` do? When might you use it?
</div>
<div class="answer">
The `trim` arguments trims a fraction of observations from each end of the vector (meaning the range) before calculating the mean.
This is useful for calculating a measure of central tendency that is robust to outliers.
</div>
### Exercise 19.5.4 {.unnumbered .exercise data-number="19.5.4"}
<div class="question">
The default value for the `method` argument to `cor()` is `c("pearson", "kendall", "spearman")`.
What does that mean? What value is used by default?
</div>
<div class="answer">
It means that the `method` argument can take one of those three values.
The first value, `"pearson"`, is used by default.
</div>
## Return values {#return-values .r4ds-section}
`r no_exercises()`
## Environment {#environment .r4ds-section}
`r no_exercises()`
[fizzbuzz]: Read [Why I’m still using “Fizz Buzz” to hire Software-Developers](https://hackernoon.com/why-im-still-using-fizz-buzz-to-hire-software-developers-7e31a89a4bbf) for more discussion on the use of the Fizz-Buzz question in programming interviews.