-
Notifications
You must be signed in to change notification settings - Fork 76
/
dry-rule.qmd
38 lines (25 loc) · 1.91 KB
/
dry-rule.qmd
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
# **D**o not **R**epeat **Y**ourself – DRY rule
## *vs.* **W**rite **E**verything **T**wice – WET rule
Following the WET rule:
* Makes changes more difficult and/or time consuming.
* Decreases code clarity.
* Increases the likelihood of inconsistencies.
To prevent duplication and follow the DRY rule, we can write custom functions.
Functions are 'self-contained' sets of commands that accomplish a specific task.
Functions usually 'take in' data or parameter values (these inputs are called 'function arguments'), process them, and 'return' a result.
You've already used several functions in this tutorial; for example `rnorm(n, mean, sd)`, where `n`, `mean`, and `sd` are inputs and the result is a random sample from the normal distribution.
The only difference here is that you are writing the function yourself.
Once a function is written, it can be used over and over again by calling its name, just like other functions such as `rnorm()`.
To write your own function, use the function `function`:
```r
AwesomeFunctionName <- function(argument1, argument2,…) {
do stuff here
}
```
To build up a function, start by writing the "`stuff`" outside the function to test that it works.
***
**YOUR TURN:**
1. Create a function that repeats the calculation of the mean of 100 values drawn from a standard normal distribution (use `mean(rnorm(n = 100))` for this calculation) `nrep` times and returns a histogram of the `nrep` means.
2. Modify your function such that, in addition to the number of repetitions `nrep`, the number of drawn values `n` (i.e. argument `n` of the `rnorm()` function) can also be varied when calling your function.
***
Note that it is useful to define the number of repetitions `nrep` outside of the function, so users of your script can more easily change that value, e.g. from a low number (to verify the script runs without error) to a large number (to obtain reliable results).