I need your help!

If you find any typos, errors, or places where the text may be improved, please let me know. The best ways to provide feedback are by GitHub or hypothes.is annotations.

Opening an issue or submitting a pull request on GitHub

Hypothesis Adding an annotation using hypothes.is. To add an annotation, select some text and then click the on the pop-up menu. To see the annotations of others, click the in the upper right-hand corner of the page.

19 Functions

19.2 When should you write a function?

Exercise 19.2.1

Why is TRUE not a parameter to rescale01()? What would happen if x contained a single missing value, and na.rm was FALSE?

The code for rescale01() is reproduced below.

If x contains a single missing value and na.rm = FALSE, then this function stills return a non-missing value.

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.

Exercise 19.2.2

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.

Exercise 19.2.3

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?

This code calculates the proportion of NA values in a vector.

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.

This code standardizes a vector so that it sums to one.

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.

This code calculates the coefficient of variation (assuming that x can only take non-negative values), which is the standard deviation divided by the mean.

I’ll write a function named coef_variation(), which takes a single argument x, and an optional na.rm argument.

Exercise 19.2.4

Follow https://nicercode.github.io/intro/writing-functions.html to write your own functions to compute the variance and skew of a numeric vector.

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:

There are multiple definitions for 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:

Exercise 19.2.5

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.

Exercise 19.2.6

What do the following functions do? Why are they useful even though they are so short?

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.

Exercise 19.2.7

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.

The lyrics of one of the most common versions 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

19.3 Functions are for humans and computers

Exercise 19.3.1

The function f1 tests whether each element of the character vector nchar starts with the string prefix. For example,

A better name for f1 is has_prefix()

The function f2 drops the last element of the vector x.

A better name for f2 is drop_last().

The function f3 repeats y once for each element of x.

Good names would include recycle() (R’s name for this behavior) or expand().

Exercise 19.3.2

Take a function that you’ve written recently and spend 5 minutes brainstorming a better name for it and its arguments.

Answer left to the reader.

Exercise 19.3.3

Compare and contrast rnorm() and MASS::mvrnorm(). How could you make them more consistent?

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.

Exercise 19.3.4

Make a case for why norm_r(), norm_d() etc would be better than rnorm(), dnorm(). Make a case for the opposite.

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.

19.4 Conditional execution

Exercise 19.4.1

What’s the difference between if and ifelse()? > Carefully read the help and construct three examples that illustrate the key differences.

The keyword if tests a single condition, while ifelse() tests each element.

Exercise 19.4.3

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.

We can use modulo operator, %%, to check divisibility. The expression x %% y returns 0 if y divides x.

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.

There are four cases to consider:

  1. If x is divisible by 3 and 5, then return “fizzbuzz”.
  2. If x is divisible by 3 and not 5, then return “fizz”.
  3. If x is divisible by 5 and not 3, then return “buzz”.
  4. 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".

This function can be slightly improved by combining the first two lines conditions so we only check whether x is divisible by 3 once.

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().

The following function is an example of a vectorized FizzBuzz function that only uses bracket assignment.

This question, called the “Fizz Buzz” question, is a common programming interview question used for screening out programmers who can’t program.[^fizzbuzz]

Exercise 19.4.4

How could you use cut() to simplify this set of nested if-else statements?

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?)

To have intervals open on the left (using <), I change the argument to right = FALSE,

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.

Exercise 19.4.5

What happens if you use switch() with numeric values?

In switch(n, ...), if n is numeric, it will return the nth 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,

If you use a non-integer number for the first argument of switch(), it will ignore the non-integer part.

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

Exercise 19.4.6

What does this switch() call do? What happens if x is "e"?

Experiment, then carefully read the documentation.

First, let’s write a function switcheroo(), and see what it returns for different values of x.

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.

19.5 Function arguments

Exercise 19.5.1

What does commas(letters, collapse = "-") do? Why?

The commas() function in the chapter is defined as

When commas() is given a collapse argument, it throws an error.

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

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.

Exercise 19.5.2

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?

This is the definition of the rule function from the chapter.

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.

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.

Note that in the second output, there is only a single - at the end.

Exercise 19.5.3

What does the trim argument to mean() do? When might you use it?

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.

Exercise 19.5.4

The default value for the method argument to cor() is c("pearson", "kendall", "spearman"). What does that mean? What value is used by default?

It means that the method argument can take one of those three values. The first value, "pearson", is used by default.

19.6 Return values

No exercises

19.7 Environment

No exercises

[fizzbuzz]: Read Why I’m still using “Fizz Buzz” to hire Software-Developers for more discussion on the use of the Fizz-Buzz question in programming interviews.