Get startedGet started for free

Default arguments

1. Default arguments

Perhaps you noticed a problem with your toss_coin function.

2. toss_coin() troubles

Unless you regularly work with crooks, you'll typically want to toss a fair coin. That is, usually you want p_head to be zero-point-five. Wouldn't it be great if you didn't have to specify it every time? Yes it would! And the good news is that this default value is easy to specify. In the function signature, you just write p_head equals zero-point-five. That's it: the body of the function doesn't change.

3. A template with defaults

Here's an update to the function template from the previous chapter. Data arguments don't take a default value, but detail arguments do.

4. Other types of default

You aren't limited to numeric defaults. Here's an example in the median function, where the na-dot-rm argument defaults to FALSE. In general, you can set a default to any type. You can even set a default to another argument! Here's an example of fromJSON, which converts JavaScript object notation into lists. Its simplifyDataFrame and simplifyMatrix arguments default to simplifyVector That is, if you specify simplifyVector, you will change all three arguments together. There are two special cases of defaults worth mentioning: NULL defaults, and categorical defaults.

5. NULL defaults

NULL is seldom used in calculations so a NULL default wouldn't usually be useful. Instead, by convention, a NULL default means that there is special handling of the argument that is too complicated to include in the function signature, and that you ought to read the documentation. set-dot-seed, used to specify a seed for R's random number generators, uses NULL defaults. In this case the docs say it means R should continue to use the same random number generation algorithm.

6. Categorical defaults

The second special case is for categorical arguments. These use a two-step process for defining the default. First you list all choices as a character vector in the signature, then you call match-dot-arg in the function body. Here's the signature for prop-dot-test, for testing the hypothesis that several groups have equal proportions. Notice it has numeric, logical, and NULL defaults like you've seen already, but the alternative argument also has a categorical default. The three choices for alternative are given as a character vector. Then, inside the body of prop-dot-test there is a line that calls match-dot-arg on alternative and reassigns it. If alternative is not specified, this will choose the first option, "two-sided".

7. Cutting a vector by quantile

In the next few exercises you'll be setting default arguments to this function, cut_by_quantile. This is a tricky chunk of code, so let's see what it does.

8. Cat heart weights

Here's a strip plot of some heart weights of cats. The weights are on the x-axis, and a bit of noise has been added to the y axis so the points don't overlap. By calling quantile, you can see the zeroth percentile - that is, the minimum - the 25th percentile, the 50th percentile which is the median, the 75th percentile, and the maximum.

9. Cutting by quantile

These quantiles allow us to split the weights into evenly sized groups. Here, red points are very low weights, green are below average, turquoise are above average, and purple are very high. The term for converting a numeric variable into a categorical variable is called "cutting". So here you've just cut the weights by quantile. In fact, cut_by_quantile is essentially this line of code, with some arguments for customization.

10. Let's practice!

Let's set some defaults.