2022-05-14 04:46:49 +08:00
# Functions {#sec-functions}
```{r}
#| results: "asis"
#| echo: false
source("_common.R")
2022-09-19 05:18:45 +08:00
status("drafting")
2022-05-14 04:46:49 +08:00
```
2015-10-21 21:04:37 +08:00
2021-02-21 23:40:40 +08:00
## Introduction
2016-07-19 21:01:50 +08:00
2021-02-21 23:40:40 +08:00
One of the best ways to improve your reach as a data scientist is to write functions.
2022-09-20 22:13:51 +08:00
Functions allow you to autofmate common tasks in a more powerful and general way than copy-and-pasting.
2021-02-21 23:40:40 +08:00
Writing a function has three big advantages over using copy-and-paste:
2016-03-03 22:25:43 +08:00
2021-02-21 23:40:40 +08:00
1. You can give a function an evocative name that makes your code easier to understand.
2016-03-03 22:25:43 +08:00
2021-02-21 23:40:40 +08:00
2. As requirements change, you only need to update code in one place, instead of many.
2016-08-10 04:49:26 +08:00
2021-02-21 23:40:40 +08:00
3. You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
2016-08-10 04:49:26 +08:00
2021-02-21 23:40:40 +08:00
Writing good functions is a lifetime journey.
2022-08-10 00:43:12 +08:00
Even after using R for many years we still learn new techniques and better ways of approaching old problems.
2022-09-20 06:46:17 +08:00
The goal of this chapter is to get you started on your journey with functions with two useful types of functions:
2016-03-03 22:25:43 +08:00
2022-09-20 06:46:17 +08:00
- Vector functions take one or more vectors as input and return a vector as output.
- Data frame functions take a data frame as input and return a data frame as output.
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
The chapter concludes with some also gives you some suggestions for how to style your functions.
2016-02-11 21:58:53 +08:00
2016-07-19 21:01:50 +08:00
### Prerequisites
2022-09-21 06:03:26 +08:00
We'll wrap up a variety of functions from around the tidyverse.
2022-09-09 00:32:10 +08:00
```{r}
2022-09-21 06:03:26 +08:00
#| message: false
2022-09-09 00:32:10 +08:00
library(tidyverse)
```
2016-07-19 21:01:50 +08:00
2022-09-19 05:18:45 +08:00
## Vector functions
2016-03-01 22:29:58 +08:00
2022-09-20 06:46:17 +08:00
We'll begin with vector functions: functions that take one or more vectors and return a vector result.
### Getting started
2021-02-21 23:40:40 +08:00
You should consider writing a function whenever you've copied and pasted a block of code more than twice (i.e. you now have three copies of the same code).
For example, take a look at this code.
What does it do?
2015-10-21 21:04:37 +08:00
```{r}
2016-08-18 21:37:48 +08:00
df <- tibble::tibble(
2015-10-21 21:04:37 +08:00
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
)
2022-09-09 00:32:10 +08:00
df |> mutate(
a = (a - min(a, na.rm = TRUE)) /
(max(a, na.rm = TRUE) - min(a, na.rm = TRUE)),
b = (b - min(b, na.rm = TRUE)) /
(max(b, na.rm = TRUE) - min(a, na.rm = TRUE)),
c = (c - min(c, na.rm = TRUE)) /
(max(c, na.rm = TRUE) - min(c, na.rm = TRUE)),
d = (d - min(d, na.rm = TRUE)) /
(max(d, na.rm = TRUE) - min(d, na.rm = TRUE))
)
2015-10-19 21:41:33 +08:00
```
2021-02-21 23:40:40 +08:00
You might be able to puzzle out that this rescales each column to have a range from 0 to 1.
But did you spot the mistake?
2022-09-20 06:46:17 +08:00
When Hadley wrote this code he made an error when copying-and-pasting and forgot to change an `a` to a `b`.
Preventing this type of mistake of is one very good reason to learn how to write functions.
2015-10-19 21:41:33 +08:00
2022-09-20 06:46:17 +08:00
To write a function you need to first analyse the code to figure out what's the same and what's different:
2015-10-21 21:04:37 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2022-09-20 06:46:17 +08:00
(a - min(a, na.rm = TRUE)) / (max(a, na.rm = TRUE) - min(a, na.rm = TRUE))
(b - min(b, na.rm = TRUE)) / (max(b, na.rm = TRUE) - min(b, na.rm = TRUE))
(c - min(c, na.rm = TRUE)) / (max(c, na.rm = TRUE) - min(c, na.rm = TRUE))
(d - min(d, na.rm = TRUE)) / (max(d, na.rm = TRUE) - min(d, na.rm = TRUE))
2015-10-21 21:04:37 +08:00
```
2022-09-20 06:46:17 +08:00
The only thing that changes on each line is the name of the variable.
That will become the argument to our function: the arguments to a function are the things that can change each time you call it.
2015-10-21 21:04:37 +08:00
2022-09-20 06:46:17 +08:00
Creating function always looks like `name <- function(arguments) body`:
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
1. You need to pick a **name** for the function.
2022-08-10 00:43:12 +08:00
Here we used `rescale01` because this function rescales a vector to lie between 0 and 1.
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
2. You list the inputs, or **arguments**, to the function inside `function`.
Here we have just one argument.
If we had more the call would look like `function(x, y, z)`.
2016-03-03 00:54:56 +08:00
2021-05-14 21:03:58 +08:00
3. You place the code you have developed in the **body** of the function, a `{` block that immediately follows `function(...)`.
2015-10-21 21:04:37 +08:00
2022-09-20 06:46:17 +08:00
```{r}
rescale01 <- function(x) {
(x - min(x, na.rm = TRUE)) / (max(x, na.rm = TRUE) - min(x, na.rm = TRUE))
}
```
2016-01-21 23:51:02 +08:00
2022-09-20 06:46:17 +08:00
At this point you might test with a few simple inputs:
2016-03-07 22:32:47 +08:00
```{r}
rescale01(c(-10, 0, 10))
rescale01(c(1, 2, 3, NA, 5))
```
2022-09-20 06:46:17 +08:00
Now we can rewrite the original code as:
2015-10-21 21:04:37 +08:00
```{r}
2022-09-09 00:32:10 +08:00
df |> mutate(
a = rescale01(a),
b = rescale01(b),
c = rescale01(c),
d = rescale01(d)
)
2015-10-21 21:04:37 +08:00
```
2022-09-20 06:46:17 +08:00
(In @sec-iteration, you'll learn how to use `across()` to reduce the duplication even further so you can write `df |> mutate(across(a:d, rescale))`).
You might notice that our function contains some duplication in this code.
We're computing the range of the data three times, so it makes sense to do it in one step using `range()` with computes both the minimum and maximum in one step:
2022-09-09 00:32:10 +08:00
```{r}
2022-09-20 06:46:17 +08:00
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
2022-09-09 00:32:10 +08:00
```
2016-01-25 22:59:36 +08:00
2022-09-20 06:46:17 +08:00
Pulling out intermediate calculations into named variables is a good practice because it makes it more clear what the code is doing.
2021-02-21 23:40:40 +08:00
Another advantage of functions is that if our requirements change, we only need to make the change in one place.
For example, we might discover that some of our variables include infinite values, and `rescale01()` fails:
2016-03-12 03:11:41 +08:00
```{r}
x <- c(1:10, Inf)
rescale01(x)
```
2016-07-15 23:14:35 +08:00
Because we've extracted the code into a function, we only need to make the fix in one place:
2016-03-12 03:11:41 +08:00
```{r}
rescale01 <- function(x) {
rng <- range(x, na.rm = TRUE, finite = TRUE)
(x - rng[1]) / (rng[2] - rng[1])
}
rescale01(x)
```
2021-02-21 23:40:40 +08:00
This is an important part of the "do not repeat yourself" (or DRY) principle.
The more repetition you have in your code, the more places you need to remember to update when things change (and they always do!), and the more likely you are to create bugs over time.
2016-03-12 03:11:41 +08:00
2022-09-20 06:46:17 +08:00
### Mutate functions
When thinking about your own functions it's useful to think about functions that return vectors of the same length as their input.
These are the sorts of functions that you'll use in `mutate()` and `filter()`.
For example, maybe instead of rescaling to 0-1 you want to rescale to mean 0 sd 1:
2022-09-19 05:18:45 +08:00
```{r}
rescale_z <- function(x) {
(x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
}
2022-09-20 06:46:17 +08:00
```
Sometimes your functions are highly specialised for one data analysis.
For example, you might have a bunch of variables that record missing values as 997, 998, or 999:
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
```{r}
2022-09-19 05:18:45 +08:00
fix_na <- function(x) {
2022-09-20 06:46:17 +08:00
if_else(x %in% c(997, 998, 999), NA, x)
2022-09-19 05:18:45 +08:00
}
2022-09-20 06:46:17 +08:00
```
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
Other cases, you might be wrapping up a simple a `case_when()` to give it a standard name:
```{r}
clamp <- function(x, min, max) {
2022-09-19 05:18:45 +08:00
case_when(
x < min ~ min,
x > max ~ max,
.default = x
)
}
2022-09-20 06:46:17 +08:00
```
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
Or maybe wrapping up some standardised string manipulation:
```{r}
2022-09-19 05:18:45 +08:00
first_upper <- function(x) {
str_sub(x, 1, 1) <- str_to_upper(str_sub(x, 1, 1))
x
}
2022-09-20 06:46:17 +08:00
# https://twitter.com/neilgcurrie/status/1571607727255834625
mape <- function(actual, predicted) {
sum(abs((actual - predicted) / actual)) / length(actual)
}
```
Another useful string manipulation function comes from NV Labor Analysis:
```{r}
# https://twitter.com/NVlabormarket/status/1571939851922198530
clean_number <- function(x) {
is_pct <- str_detect(x, "%")
num <- num |>
str_remove_all("%") |>
str_remove_all(x, ",") |>
str_remove_all(x, fixed("$")) |>
as.numeric(num)
if_else(is_pct, num / 100, num)
}
2022-09-19 05:18:45 +08:00
```
### Summary functions
2022-09-20 06:46:17 +08:00
In other cases you want a function that returns a single value for use in `summary()`.
Sometimes this can just be a matter of setting a default argument:
```{r}
commas <- function(x) {
str_flatten(x, collapse = ", ")
}
```
Or some very simple computation, for example to compute the coefficient of variation, which standardises the standard deviation by dividing it by the mean:
2022-09-19 05:18:45 +08:00
```{r}
cv <- function(x, na.rm = FALSE) {
sd(x, na.rm = na.rm) / mean(x, na.rm = na.rm)
}
2022-09-20 06:46:17 +08:00
```
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
Or maybe you just want to give a common pattern a name that's easier to remember:
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
```{r}
# https://twitter.com/gbganalyst/status/1571619641390252033
n_missing <- function(x) {
sum(is.na(x))
}
2022-09-19 05:18:45 +08:00
```
2020-01-16 02:40:59 +08:00
### Exercises
2016-01-25 22:59:36 +08:00
2021-02-21 23:40:40 +08:00
1. Why is `TRUE` not a parameter to `rescale01()`?
What would happen if `x` contained a single missing value, and `na.rm` was `FALSE`?
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
2. In the second variant of `rescale01()`, infinite values are left unchanged.
2022-09-20 06:46:17 +08:00
Can you rewrite `rescale01()` so that `-Inf` is mapped to 0, and `Inf` is mapped to 1?
2016-08-10 04:49:26 +08:00
2021-02-21 23:40:40 +08:00
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?
2016-02-13 06:05:25 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-02-13 06:05:25 +08:00
mean(is.na(x))
2021-02-21 23:40:40 +08:00
2016-02-13 06:05:25 +08:00
x / sum(x, na.rm = TRUE)
2021-02-21 23:40:40 +08:00
2016-03-03 00:54:56 +08:00
```
2021-02-21 23:40:40 +08:00
4. Write your own functions to compute the variance and skewness of a numeric vector.
Variance is defined as $$
2020-01-16 02:41:22 +08:00
\mathrm{Var}(x) = \frac{1}{n - 1} \sum_{i=1}^n (x_i - \bar{x}) ^2 \text{,}
2021-02-21 23:40:40 +08:00
$$ where $\bar{x} = (\sum_i^n x_i) / n$ is the sample mean.
Skewness is defined as $$
2020-01-16 02:41:22 +08:00
\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{.}
$$
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
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.
6. What do the following functions do?
Why are they useful even though they are so short?
2016-03-03 00:54:56 +08:00
```{r}
is_directory <- function(x) file.info(x)$isdir
is_readable <- function(x) file.access(x, 4) == 0
2016-02-13 06:05:25 +08:00
```
2021-02-21 23:40:40 +08:00
7. Read the [complete lyrics](https://en.wikipedia.org/wiki/Little_Bunny_Foo_Foo) 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.
2016-01-25 22:59:36 +08:00
2022-09-20 06:46:17 +08:00
## Data frame functions
2022-09-21 06:03:26 +08:00
The second common form of function takes a data frame as the first argument, some extra arguments that say what to do with it, and returns a data frame.
There are lots of functions of this nature, but we'll focus on wrapping tidyverse functions, principally those from dplyr and tidyr.
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
### Tidy evaluation
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
Let's illustrate the problem with a very simple function: `pull_unique()`.
The goal of this function is to `pull()` the unique (distinct) values of a variable:
2022-09-19 05:18:45 +08:00
```{r}
2022-09-21 06:03:26 +08:00
pull_unique <- function(df, var) {
df |>
distinct(var) |>
pull(var)
2022-09-19 05:18:45 +08:00
}
```
2022-09-21 06:03:26 +08:00
If we try and use it, we get an error:
2022-09-20 06:46:17 +08:00
```{r}
2022-09-21 06:03:26 +08:00
#| error: true
diamonds |> pull_unique(clarity)
2022-09-20 06:46:17 +08:00
```
2022-09-21 06:03:26 +08:00
To make the problem a bit more clear we can use a made up data frame:
2022-09-20 06:46:17 +08:00
```{r}
2022-09-21 06:03:26 +08:00
df <- tibble(var = "var", x = "x", y = "y")
df |> pull_unique(x)
df |> pull_unique(y)
2022-09-20 06:46:17 +08:00
```
2022-09-21 06:03:26 +08:00
The problem is that regardless of the inputs, our function is always doing literally `df |> distinct(var) |> pull(var)`, instead of `df |> distinct(x) |> pull(x)` or `df |> distinct(y) |> pull(y)`.
This is a problem of indirection, and it arises because dplyr allows you to refer to the names of variables inside your data frame without any special treatment, so called **tidy evaluation**.
Tidy evaluation is great 95% of the time because it makes our data analyses very concise as we never have to say which data frame a variable comes from; it's obvious from the context.
The downside of tidy evaluation comes when we want to wrap up repeated tidyverse code into a function: we need some way tell `distinct()` and `pull()` not to treat `var` as the name of a variable, but instead look inside `var` for the variable we actually want to use.
The solution to this problem is **embracing**.
By wrapping a variable in `{{ }}` (embracing it) dplyr knows that we want to use the value stored inside that variable.
One way to remember what's happening is to think of `{{ }}` like looking down a tunnel --- it's going to make the function look inside of `var` rather than looking for a variable called `var`.
To make `pull_unique()` work we just need to replace `var` with `{{ var }}`:
2022-09-20 06:46:17 +08:00
```{r}
2022-09-21 06:03:26 +08:00
pull_unique <- function(df, var) {
2022-09-20 06:46:17 +08:00
df |>
2022-09-21 06:03:26 +08:00
distinct({{ var }}) |>
pull({{ var }})
2022-09-20 06:46:17 +08:00
}
2022-09-21 06:03:26 +08:00
diamonds |> pull_unique(clarity)
2022-09-20 06:46:17 +08:00
```
2022-09-21 06:03:26 +08:00
### When to embrace?
2022-09-19 05:18:45 +08:00
2022-09-21 06:03:26 +08:00
So the art of wrapping tidyverse functions basically figuring out which arguments need to be embraced.
Fortunately this is pretty easy because you can look it up from the documentation 😄.
There are two terms to look for in the docs:
2022-09-19 05:18:45 +08:00
2022-09-21 06:03:26 +08:00
- **Data-masking**: this is used in functions like `arrange()`, `filter()`, and `summarise()` which do computation with variables.
- **Tidy-selections**: this is used for for functions like `select()`, `relocate()`, and `rename()` that work with groups of variables.
2022-09-19 05:18:45 +08:00
2022-09-21 06:03:26 +08:00
TODO: something about ...
2022-09-19 05:18:45 +08:00
2022-09-21 06:03:26 +08:00
Your intuition for many common functions should be pretty good --- think about whether it's ok to compute `x + 1` or select multiple variables with `a:x`.
There are are some that are harder to tell because you usually use them with a single variable, so it's hard to tell whether they're data-masking or tidy-select:
2022-09-19 05:18:45 +08:00
2022-09-20 06:46:17 +08:00
- The arguments to `group_by()`, `count()`, and `distinct()` are computing arguments because they can all create new variables.
- The `names_from` arguments to `pivot_wider()` is a selecting function because you can take the names from multiple variables with `names_from = c(x, y, z)`.
2022-09-21 06:03:26 +08:00
- It's not a data frame function, but ggplot2's `aes()` uses data-masking because `aes(x * 2, y / 10)` etc.
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
In the next two sections we'll explore the sorts of handy functions you might write for data-masking and tidy-select arguments
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
### Data-masking examples
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
If you commonly perform the same set of summaries when doing initial data exploration, you might consider wrapping them up in a helper function:
2022-09-20 06:46:17 +08:00
```{r}
2022-09-21 06:03:26 +08:00
summary6 <- function(data, var) {
2022-09-20 06:46:17 +08:00
data %>% summarise(
2022-09-21 06:03:26 +08:00
min = min({{ var }}, na.rm = TRUE),
mean = mean({{ var }}, na.rm = TRUE),
median = median({{ var }}, na.rm = TRUE),
max = max({{ var }}, na.rm = TRUE),
n = n(),
n_miss = sum(is.na({{ var }}))
2022-09-20 06:46:17 +08:00
)
}
2022-09-21 06:03:26 +08:00
diamonds |> summary6(carat)
```
The nice thing about this function is because it wraps summary you can used it on grouped data:
```{r}
diamonds |>
group_by(cut) |>
summary6(carat)
2022-09-20 06:46:17 +08:00
```
2022-09-21 06:03:26 +08:00
Because the arguments to summarize are data-masking that also means that the `var` argument to `summary6()` is data-masking.
That means you can also summarize computed variables:
```{r}
diamonds |>
group_by(cut) |>
summary6(log10(carat))
```
To summarize multiple you'll need wait until @sec-across, where you'll learn about `across()` which lets you repeat the same computations with multiple variables.
Another common helper function is to write a version of `count()` that also computes proportions:
2022-09-20 06:46:17 +08:00
```{r}
# https://twitter.com/Diabb6/status/1571635146658402309
2022-09-21 06:03:26 +08:00
count_prop <- function(df, var, sort = FALSE) {
2022-09-20 06:46:17 +08:00
df |>
2022-09-21 06:03:26 +08:00
count({{ var }}, sort = sort) |>
mutate(prop = n / sum(n))
2022-09-20 06:46:17 +08:00
}
2022-09-21 06:03:26 +08:00
diamonds |> count_prop(clarity)
2022-09-20 06:46:17 +08:00
```
2022-09-21 06:03:26 +08:00
Note that this function has three arguments: `df`, `var`, and `sort`, and only `var` needs to be embraced because it's passed to `count()` which uses data-masking for all variables in `…`.
Or maybe you want to find the unique values for a variable for a subset of the data:
2022-09-20 06:46:17 +08:00
```{r}
2022-09-21 06:03:26 +08:00
unique_where <- function(df, condition, var) {
df |>
filter({{ condition }}) |>
distinct({{ var }}) |>
arrange({{ var }}) |>
pull()
}
nycflights13::flights |> unique_where(month == 12, dest)
```
### Tidy-select arguments
```{r}
#| include: false
pick <- function(cols) {
across({{ cols }})
}
```
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
```{r}
# https://twitter.com/drob/status/1571879373053259776
enrich_join <- function(x, y, y_vars = everything(), by = NULL) {
left_join(x, y |> select({{ y_vars }}), by = by)
}
```
Another useful helper is to make a "wide" count, where you make a 2d table of counts.
```{r}
2022-09-20 06:46:17 +08:00
# Inspired by https://twitter.com/pollicipes/status/1571606508944719876
count_wide <- function(data, rows, cols) {
data |>
count(pick(c({{rows}}, {{cols}}))) |>
pivot_wider(names_from = {{cols}}, values_from = n)
}
mtcars |> count_wide(vs, cyl)
mtcars |> count_wide(c(vs, am), cyl)
```
This requires use `pick()` to use tidy-select inside a data-masking (`count()`) function.
2022-09-21 06:03:26 +08:00
### Learning more
2022-09-20 06:46:17 +08:00
2022-09-21 06:03:26 +08:00
Once you have the basics under your belt, you can learn more about the full range of tidy evaluation possibilities by reading `vignette("programming", package = "dplyr")`.
2022-09-19 05:18:45 +08:00
## Style
2016-01-25 22:59:36 +08:00
2021-02-21 23:40:40 +08:00
It's important to remember that functions are not just for the computer, but are also for humans.
R doesn't care what your function is called, or what comments it contains, but these are important for human readers.
This section discusses some things that you should bear in mind when writing functions that humans can understand.
2016-03-03 00:54:56 +08:00
2022-09-19 05:18:45 +08:00
Excerpt from <https://style.tidyverse.org/functions.html>
### Names
2021-02-21 23:40:40 +08:00
The name of a function is important.
Ideally, the name of your function will be short, but clearly evoke what the function does.
That's hard!
But it's better to be clear than short, as RStudio's autocomplete makes it easy to type long names.
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
Generally, function names should be verbs, and arguments should be nouns.
There are some exceptions: nouns are ok if the function computes a very well known noun (i.e. `mean()` is better than `compute_mean()`), or accessing some property of an object (i.e. `coef()` is better than `get_coefficients()`).
A good sign that a noun might be a better choice is if you're using a very broad verb like "get", "compute", "calculate", or "determine".
Use your best judgement and don't be afraid to rename a function if you figure out a better name later.
2016-03-08 22:15:54 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-03-08 22:15:54 +08:00
# Too short
f()
# Not a verb, or descriptive
my_awesome_function()
2016-03-03 00:54:56 +08:00
2016-03-08 22:15:54 +08:00
# Long, but clear
impute_missing()
collapse_years()
```
2016-03-03 22:25:43 +08:00
2022-09-19 05:18:45 +08:00
### Indenting
2016-03-04 23:30:16 +08:00
2021-02-21 23:40:40 +08:00
Both `if` and `function` should (almost) always be followed by squiggly brackets (`{}`), and the contents should be indented by two spaces.
This makes it easier to see the hierarchy in your code by skimming the left-hand margin.
2016-03-08 22:15:54 +08:00
2021-02-21 23:40:40 +08:00
An opening curly brace should never go on its own line and should always be followed by a new line.
A closing curly brace should always go on its own line, unless it's followed by `else`.
Always indent the code inside curly braces.
2016-03-03 00:54:56 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-03-03 00:54:56 +08:00
# Good
if (y < 0 && debug) {
message("Y is negative")
}
if (y == 0) {
log(x)
} else {
y ^ x
}
# Bad
if (y < 0 && debug)
message("Y is negative")
if (y == 0) {
log(x)
}
else {
y ^ x
2016-01-25 22:59:36 +08:00
}
```
2016-03-03 22:25:43 +08:00
### Exercises
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
1. What's the difference between `if` and `ifelse()`?
Carefully read the help and construct three examples that illustrate the key differences.
2016-03-03 00:54:56 +08:00
2021-02-21 23:40:40 +08:00
2. 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.)
2016-03-04 23:30:16 +08:00
2021-02-21 23:40:40 +08:00
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".
2022-09-09 04:20:53 +08:00
Otherwise, it returns the number itself.
2021-02-21 23:40:40 +08:00
Make sure you first write working code before you create the function.
4. How could you use `cut()` to simplify this set of nested if-else statements?
2016-03-04 23:30:16 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-03-04 23:30:16 +08:00
if (temp <= 0) {
"freezing"
} else if (temp <= 10) {
"cold"
} else if (temp <= 20) {
"cool"
} else if (temp <= 30) {
"warm"
} else {
"hot"
}
```
2021-02-21 23:40:40 +08:00
2022-08-10 00:43:12 +08:00
How would you change the call to `cut()` if we used `<` instead of `<=`?
2021-02-21 23:40:40 +08:00
What is the other chief advantage of `cut()` for this problem?
(Hint: what happens if you have many values in `temp`?)
2016-03-04 23:30:16 +08:00
2021-02-21 23:40:40 +08:00
5. What happens if you use `switch()` with numeric values?
2016-03-03 00:55:14 +08:00
2021-02-21 23:40:40 +08:00
6. What does this `switch()` call do?
What happens if `x` is "e"?
2016-03-03 00:55:14 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-03-03 22:25:43 +08:00
switch(x,
a = ,
b = "ab",
c = ,
d = "cd"
)
```
2021-02-21 23:40:40 +08:00
Experiment, then carefully read the documentation.
2016-03-03 00:55:14 +08:00
2022-09-19 05:18:45 +08:00
### Exercises
2016-03-03 22:25:43 +08:00
2022-09-19 05:18:45 +08:00
1. Read the source code for each of the following three functions, puzzle out what they do, and then brainstorm better names.
2016-03-03 00:54:56 +08:00
2022-09-19 05:18:45 +08:00
```{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))
}
```
2016-03-03 00:54:56 +08:00
2022-09-19 05:18:45 +08:00
2. Take a function that you've written recently and spend 5 minutes brainstorming a better name for it and its arguments.
2016-03-03 00:54:56 +08:00
2022-09-19 05:18:45 +08:00
3. Compare and contrast `rnorm()` and `MASS::mvrnorm()`.
How could you make them more consistent?
2022-05-14 04:46:49 +08:00
2022-09-19 05:18:45 +08:00
4. Make a case for why `norm_r()`, `norm_d()` etc would be better than `rnorm()`, `dnorm()`.
Make a case for the opposite.
2016-03-03 22:25:43 +08:00
2022-09-19 05:18:45 +08:00
## Learning more
2016-03-03 00:54:56 +08:00
2022-09-19 05:18:45 +08:00
### Conditional execution {#sec-conditional-execution}
2016-03-03 22:25:43 +08:00
2022-09-19 05:18:45 +08:00
An `if` statement allows you to conditionally execute code.
It looks like this:
2016-03-03 22:25:43 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2022-09-19 05:18:45 +08:00
if (condition) {
# code executed when condition is TRUE
} else {
# code executed when condition is FALSE
2016-03-08 22:15:54 +08:00
}
```
2016-03-07 22:32:47 +08:00
2022-09-19 05:18:45 +08:00
To get help on `if` you need to surround it in backticks: `` ?`if` ``.
The help isn't particularly helpful if you're not already an experienced programmer, but at least you know how to get to it!
2016-03-08 22:15:54 +08:00
2022-09-19 05:18:45 +08:00
Here's a simple function that uses an `if` statement.
The goal of this function is to return a logical vector describing whether or not each element of a vector is named.
2016-03-08 22:15:54 +08:00
```{r}
2022-09-19 05:18:45 +08:00
has_name <- function(x) {
nms <- names(x)
if (is.null(nms)) {
rep(FALSE, length(x))
} else {
!is.na(nms) & nms != ""
2016-03-08 22:15:54 +08:00
}
}
```
2022-09-19 05:18:45 +08:00
You can use `||` (or) and `&&` (and) to combine multiple logical expressions.
These operators are "short-circuiting": as soon as `||` sees the first `TRUE` it returns `TRUE` without computing anything else.
As soon as `&&` sees the first `FALSE` it returns `FALSE`.
2016-03-08 22:15:54 +08:00
2022-09-19 05:18:45 +08:00
This function takes advantage of the standard return rule: a function returns the last value that it computed.
Here that is either one of the two branches of the `if` statement.
2016-03-08 22:15:54 +08:00
2022-09-19 05:18:45 +08:00
The `condition` must evaluate to either `TRUE` or `FALSE`.
If it's not; you'll get an error.
2016-08-10 04:49:26 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| error: true
2022-09-19 05:18:45 +08:00
if (c(TRUE, FALSE)) {}
2016-03-09 22:42:51 +08:00
2022-09-19 05:18:45 +08:00
if (NA) {}
2016-03-09 22:42:51 +08:00
```
2022-09-19 05:18:45 +08:00
You should never use `|` or `&` in an `if` statement: these are vectorised operations that apply to multiple values (that's why you use them in `filter()`).
If you do have a logical vector, you can use `any()` or `all()` to collapse it to a single value.
Be careful when testing for equality.
`==` is vectorised, which means that it's easy to get more than one output.
Either check the length is already 1, collapse with `all()` or `any()`.
2016-02-11 21:58:53 +08:00
2022-09-19 05:18:45 +08:00
You can chain multiple if statements together:
2016-02-11 21:58:53 +08:00
2022-05-14 04:46:49 +08:00
```{r}
#| eval: false
2016-02-11 21:58:53 +08:00
2022-09-19 05:18:45 +08:00
if (this) {
# do that
} else if (that) {
# do something else
} else {
#
2016-02-13 06:05:25 +08:00
}
```
2022-09-19 05:18:45 +08:00
###