Chapter 15 Programming in R

This unit covers some more advanced programming in R - namely:

  1. Conditional Flow
  2. Functions
  3. Iteration

Mastering these skills will make you virtually invincible in R!

Note that these concepts are not specific to R. While the syntax might vary, the basic idea of flow, functions, and iteration are common across all scripting languages. So if you ever think of picking up Python or something else, it is critical to familiarize yourself with these concepts.

15.1 Conditional Flow

Sometimes you only want to execute code if a certain condition is met. To do that, we use an if-else statement. It looks like this:

if (condition) {
  # Code executed when condition is TRUE
} else {
  # Code executed when condition is FALSE
}

condition is a statement that must always evaluate to either TRUE or FALSE. This is similar to filter(), except condition can only be a single value (i.e., a vector of length 1), whereas filter() works for entire vectors (or columns).

Let's look at a simple example:

age = 84
if (age > 60) {
    print("OK Boomer")
} else {
    print("But you don't look like a professor!")
}
#> [1] "OK Boomer"

We refer to the first print command as the first branch.

Let's change the age variable to execute the second branch:

age = 20
if (age > 60) {
    print("OK Boomer")
} else {
    print("But you don't look like a professor!")
}
#> [1] "But you don't look like a professor!"

15.1.1 Multiple Conditions

You can chain conditional statements together:

if (this) {
  # Do that
} else if (that) {
  # Do something else
} else {
  # Do something completely different
}

15.1.2 Complex Statements

We can generate more complex conditional statements with Boolean operators like & and |:

age = 45 

if (age > 60) {
    print("OK Boomer")
} else if (age < 60 & age > 40) {
    print("How's the midlife crisis?")
} else {
    print("But you don't look like a professor!")
}
#> [1] "How's the midlife crisis?"

15.1.3 Code Style

Both if and function should (almost) always be followed by squiggly brackets ({}), and the contents should be indented. This makes it easier to see the hierarchy in your code by skimming the left-hand margin.

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 is followed by else. Always indent the code inside curly braces.

# Bad
if (y < 0 && debug)
message("Y is negative")

if (y == 0) {
  log(x)
} 
else {
  y ^ x
}

# Good
if (y < 0 && debug) {
  message("Y is negative")
}

if (y == 0) {
  log(x)
} else {
  y ^ x
}

15.1.4 if vs. if_else

Because if-else conditional statements like the ones outlined above must always resolve to a single TRUE or FALSE, they cannot be used for vector operations. Vector operations are where you make multiple comparisons simultaneously for each value stored inside a vector.

Consider the gapminder data and imagine you wanted to create a new column identifying whether or not a country-year observation has a life expectancy of at least 35.

gap <- gapminder
head(gap)
#> # A tibble: 6 x 6
#>   country     continent  year lifeExp      pop gdpPercap
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#> 1 Afghanistan Asia       1952    28.8  8425333      779.
#> 2 Afghanistan Asia       1957    30.3  9240934      821.
#> 3 Afghanistan Asia       1962    32.0 10267083      853.
#> 4 Afghanistan Asia       1967    34.0 11537966      836.
#> 5 Afghanistan Asia       1972    36.1 13079460      740.
#> 6 Afghanistan Asia       1977    38.4 14880372      786.

This sounds like a classic if-else operation. For each observation, if lifeExp is greater than or equal to 35, then the value in the new column should be 1. Otherwise, it should be 0. But what happens if we try to implement this using an if-else operation like above?

gap_if <- gap %>%
   mutate(life.35 = if(lifeExp >= 35){
     1
   } else {
     0
   })
#> Warning: Problem with `mutate()` input `life.35`.
#> ℹ the condition has length > 1 and only the first element will be used
#> ℹ Input `life.35` is `if (...) NULL`.
#> Warning in if (lifeExp >= 35) {: the condition has length > 1 and only the first
#> element will be used

head(gap_if)
#> # A tibble: 6 x 7
#>   country     continent  year lifeExp      pop gdpPercap life.35
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>   <dbl>
#> 1 Afghanistan Asia       1952    28.8  8425333      779.       0
#> 2 Afghanistan Asia       1957    30.3  9240934      821.       0
#> 3 Afghanistan Asia       1962    32.0 10267083      853.       0
#> 4 Afghanistan Asia       1967    34.0 11537966      836.       0
#> 5 Afghanistan Asia       1972    36.1 13079460      740.       0
#> 6 Afghanistan Asia       1977    38.4 14880372      786.       0

This did not work correctly. Because if() can only handle a single TRUE/FALSE value, it only checked the first row of the data frame. That row contained 28.801, so it generated a vector of length 1704 with each value being 0.

Because we in fact want to make this if-else comparison 1704 times, we should instead use if_else(). This vectorizes the if-else comparison and makes a separate comparison for each row of the data frame. This allows us to correctly generate this new column.

gap_ifelse <- gap %>%
  mutate(life.35 = if_else(lifeExp >= 35, 1, 0))

gap_ifelse
#> # A tibble: 1,704 x 7
#>   country     continent  year lifeExp      pop gdpPercap life.35
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>   <dbl>
#> 1 Afghanistan Asia       1952    28.8  8425333      779.       0
#> 2 Afghanistan Asia       1957    30.3  9240934      821.       0
#> 3 Afghanistan Asia       1962    32.0 10267083      853.       0
#> 4 Afghanistan Asia       1967    34.0 11537966      836.       0
#> 5 Afghanistan Asia       1972    36.1 13079460      740.       1
#> 6 Afghanistan Asia       1977    38.4 14880372      786.       1
#> # … with 1,698 more rows
library(tidyverse)
library(gapminder)

15.2 Functions

Functions are the basic building blocks of programs. Think of them as "mini-scripts" or "tiny commands." We have already used dozens of functions created by others (e.g., filter() and mean()).

This lesson teaches you how to write you own functions and why you would want to do so. The details are pretty simple, but this is one of those ideas where it is good to get lots of practice!

15.2.1 Why Write Functions?

Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. For example, take a look at the following code:

gap <- gapminder

gap_norm <- gap %>%
  mutate(pop_norm = (pop - min(pop)) / (max(pop) - min (pop)),
         gdp_norm = (gdpPercap - min(gdpPercap)) / (max(gdpPercap) - min (gdpPercap)),
         life_norm = (lifeExp - min(lifeExp) / (max(pop)) - min (lifeExp)))

summary(gap_norm$pop_norm)

You might be able to puzzle out that this rescales each numeric column to have a range from 0 to 1. But did you spot the mistakes? I made two errors when copying-and-pasting the code for lifeExp.

Functions have a number of advantages over this "copy-and-paste" approach:

  • They are easy to reuse. If you need to change things, you only have to update code in one place instead of many.

  • They are self-documenting. Functions name pieces of code the way variables name strings and numbers. Give your function a good name and you will easily remember the function and its purpose.

  • They are easier to debug. There are fewer chances to make mistakes, because the code only exists in one location (i.e., updating a variable name in one place, but not in another).

15.2.2 Anatomy of a Function

Functions have three key components:

  1. A name. This should be informative and describe what the function does.

  2. The arguments, or list of inputs, to the function. They go inside the parentheses in function().

  3. The body. This is the block of code within {} that immediately follows function(...), and it is the code that you develop to perform the action described in the name using the arguments you provide.

my_function <- function(x, y){
  # do
  # something
  # here
  return(result)
}

In this example, my_function is the name of the function, x and y are the arguments, and the stuff inside the {} is the body.

15.2.3 Writing a Function

Let's re-write the scaling code above as a function. To write a function, you need to first analyze the code. How many inputs does it have?

# The corrected code
gap <- gapminder

gap_norm <- gap %>%
  mutate(pop_norm = (pop - min(pop)) / (max(pop) - min (pop)),
         gdp_norm = (gdpPercap - min(gdpPercap)) / (max(gdpPercap) - min (gdpPercap)),
         life_norm = (lifeExp - min(lifeExp)) / (max(lifeExp) - min (lifeExp)))

# Focus on the line
# pop_norm = (pop - min(pop)) / (max(pop) - min (pop))

This code only has one input: gap$pop. To make the inputs more clear, it is a good idea to rewrite the code using temporary variables with general names. Here this code only requires a single numeric vector, which I will call x:

x <- gap$pop

(x - min(x)) / (max(x) - min(x))
#>    [1] 6.34e-03 6.96e-03 7.74e-03 8.70e-03 9.87e-03 1.12e-02 9.72e-03 1.05e-02
#>    [9] 1.23e-02 1.68e-02 1.91e-02 2.41e-02 9.27e-04 1.07e-03 1.27e-03 1.46e-03
#>   [17] 1.67e-03 1.86e-03 2.06e-03 2.29e-03 2.48e-03 2.55e-03 2.62e-03 2.69e-03
#>   [25] 6.99e-03 7.74e-03 8.30e-03 9.63e-03 1.11e-02 1.30e-02 1.51e-02 1.76e-02
#>   [33] 1.99e-02 2.20e-02 2.37e-02 2.52e-02 3.16e-03 3.41e-03 3.61e-03 3.93e-03
#>   [41] 4.42e-03 4.63e-03 5.28e-03 5.93e-03 6.58e-03 7.44e-03 8.19e-03 9.37e-03
#>   [49] 1.35e-02 1.48e-02 1.61e-02 1.73e-02 1.87e-02 2.04e-02 2.22e-02 2.39e-02
#>   [57] 2.57e-02 2.74e-02 2.90e-02 3.05e-02 6.55e-03 7.32e-03 8.14e-03 8.96e-03
#>   [65] 9.95e-03 1.06e-02 1.15e-02 1.23e-02 1.32e-02 1.40e-02 1.48e-02 1.55e-02
#>   [73] 5.21e-03 5.24e-03 5.36e-03 5.55e-03 5.68e-03 5.69e-03 5.70e-03 5.70e-03
#>   [81] 5.96e-03 6.07e-03 6.13e-03 6.17e-03 4.58e-05 5.96e-05 8.48e-05 1.08e-04
#>   [89] 1.30e-04 1.80e-04 2.41e-04 2.99e-04 3.56e-04 4.08e-04 4.52e-04 4.92e-04
#>   [97] 3.55e-02 3.89e-02 4.31e-02 4.76e-02 5.36e-02 6.09e-02 7.05e-02 7.86e-02
#>  [105] 8.62e-02 9.35e-02 1.03e-01 1.14e-01 6.58e-03 6.77e-03 6.95e-03 7.20e-03
#>  [113] 7.32e-03 7.40e-03 7.43e-03 7.44e-03 7.57e-03 7.69e-03 7.77e-03 7.84e-03
#>  [121] 1.27e-03 1.41e-03 1.59e-03 1.80e-03 2.05e-03 2.36e-03 2.72e-03 3.17e-03
#>  [129] 3.73e-03 4.55e-03 5.28e-03 6.08e-03 2.14e-03 2.39e-03 2.68e-03 3.02e-03
#>  [137] 3.42e-03 3.81e-03 4.23e-03 4.62e-03 5.18e-03 5.79e-03 6.36e-03 6.87e-03
#>  [145] 2.07e-03 2.29e-03 2.49e-03 2.67e-03 2.85e-03 3.05e-03 3.12e-03 3.25e-03
#>  [153] 3.18e-03 2.69e-03 3.11e-03 3.41e-03 2.90e-04 3.14e-04 3.43e-04 3.74e-04
#>  [161] 4.24e-04 5.47e-04 6.90e-04 8.28e-04 9.73e-04 1.12e-03 1.19e-03 1.20e-03
#>  [169] 4.29e-02 4.97e-02 5.76e-02 6.67e-02 7.64e-02 8.66e-02 9.78e-02 1.08e-01
#>  [177] 1.18e-01 1.28e-01 1.36e-01 1.44e-01 5.47e-03 5.76e-03 6.03e-03 6.26e-03
#>  [185] 6.46e-03 6.63e-03 6.70e-03 6.76e-03 6.52e-03 6.07e-03 5.76e-03 5.51e-03
#>  [193] 3.34e-03 3.53e-03 3.69e-03 3.84e-03 4.08e-03 4.42e-03 4.99e-03 5.71e-03
#>  [201] 6.69e-03 7.81e-03 9.25e-03 1.08e-02 1.81e-03 1.98e-03 2.20e-03 2.48e-03
#>  [209] 2.63e-03 2.86e-03 3.43e-03 3.84e-03 4.36e-03 4.60e-03 5.28e-03 6.32e-03
#>  [217] 3.51e-03 3.99e-03 4.57e-03 5.23e-03 5.60e-03 5.25e-03 5.47e-03 6.30e-03
#>  [225] 7.65e-03 8.89e-03 9.76e-03 1.07e-02 3.75e-03 4.02e-03 4.35e-03 4.76e-03
#>  [233] 5.28e-03 5.99e-03 6.97e-03 8.13e-03 9.41e-03 1.07e-02 1.20e-02 1.34e-02
#>  [241] 1.12e-02 1.29e-02 1.44e-02 1.57e-02 1.69e-02 1.80e-02 1.91e-02 2.01e-02
#>  [249] 2.16e-02 2.29e-02 2.41e-02 2.53e-02 9.34e-04 1.01e-03 1.11e-03 1.27e-03
#>  [257] 1.42e-03 1.60e-03 1.83e-03 2.11e-03 2.43e-03 2.76e-03 3.02e-03 3.27e-03
#>  [265] 1.99e-03 2.15e-03 2.34e-03 2.61e-03 2.91e-03 3.28e-03 3.65e-03 4.12e-03
#>  [273] 4.83e-03 5.69e-03 6.66e-03 7.72e-03 4.79e-03 5.30e-03 5.99e-03 6.67e-03
#>  [281] 7.32e-03 7.99e-03 8.67e-03 9.41e-03 1.02e-02 1.10e-02 1.17e-02 1.23e-02
#>  [289] 4.22e-01 4.83e-01 5.05e-01 5.72e-01 6.54e-01 7.15e-01 7.59e-01 8.22e-01
#>  [297] 8.83e-01 9.33e-01 9.71e-01 1.00e+00 9.32e-03 1.09e-02 1.29e-02 1.49e-02
#>  [305] 1.71e-02 1.90e-02 2.10e-02 2.34e-02 2.59e-02 2.85e-02 3.11e-02 3.35e-02
#>  [313] 7.12e-05 8.41e-05 9.99e-05 1.19e-04 1.44e-04 1.86e-04 2.19e-04 2.54e-04
#>  [321] 2.99e-04 3.55e-04 4.20e-04 4.94e-04 1.06e-02 1.18e-02 1.32e-02 1.51e-02
#>  [329] 1.74e-02 2.00e-02 2.32e-02 2.69e-02 3.16e-02 3.62e-02 4.20e-02 4.90e-02
#>  [337] 6.03e-04 6.68e-04 7.49e-04 8.49e-04 9.71e-04 1.12e-03 1.30e-03 1.52e-03
#>  [345] 1.78e-03 2.08e-03 2.48e-03 2.84e-03 6.57e-04 7.98e-04 9.75e-04 1.16e-03
#>  [353] 1.35e-03 1.55e-03 1.79e-03 2.08e-03 2.36e-03 2.62e-03 2.86e-03 3.09e-03
#>  [361] 2.21e-03 2.46e-03 2.86e-03 3.55e-03 4.56e-03 5.61e-03 6.80e-03 8.12e-03
#>  [369] 9.64e-03 1.10e-02 1.23e-02 1.36e-02 2.90e-03 2.98e-03 3.05e-03 3.12e-03
#>  [377] 3.16e-03 3.23e-03 3.30e-03 3.36e-03 3.36e-03 3.33e-03 3.35e-03 3.36e-03
#>  [385] 4.51e-03 4.99e-03 5.46e-03 6.13e-03 6.65e-03 7.19e-03 7.38e-03 7.72e-03
#>  [393] 8.09e-03 8.28e-03 8.47e-03 8.61e-03 6.87e-03 7.17e-03 7.25e-03 7.41e-03
#>  [401] 7.43e-03 7.66e-03 7.77e-03 7.77e-03 7.78e-03 7.77e-03 7.73e-03 7.71e-03
#>  [409] 3.24e-03 3.36e-03 3.48e-03 3.62e-03 3.74e-03 3.81e-03 3.84e-03 3.84e-03
#>  [417] 3.88e-03 3.96e-03 4.03e-03 4.10e-03 2.38e-06 8.98e-06 2.27e-05 5.13e-05
#>  [425] 9.01e-05 1.28e-04 1.87e-04 1.90e-04 2.46e-04 2.71e-04 2.94e-04 3.31e-04
#>  [433] 1.84e-03 2.17e-03 2.57e-03 3.03e-03 3.50e-03 3.98e-03 4.48e-03 5.00e-03
#>  [441] 5.53e-03 6.02e-03 6.51e-03 7.02e-03 2.65e-03 3.03e-03 3.50e-03 4.07e-03
#>  [449] 4.73e-03 5.47e-03 6.30e-03 7.19e-03 8.11e-03 8.99e-03 9.75e-03 1.04e-02
#>  [457] 1.68e-02 1.89e-02 2.13e-02 2.40e-02 2.64e-02 2.94e-02 3.46e-02 4.00e-02
#>  [465] 4.50e-02 5.01e-02 5.56e-02 6.08e-02 1.50e-03 1.74e-03 2.04e-03 2.41e-03
#>  [473] 2.83e-03 3.20e-03 3.35e-03 3.63e-03 3.95e-03 4.34e-03 4.77e-03 5.22e-03
#>  [481] 1.19e-04 1.31e-04 1.43e-04 1.52e-04 1.65e-04 1.01e-04 1.71e-04 2.13e-04
#>  [489] 2.49e-04 2.88e-04 3.30e-04 3.73e-04 1.05e-03 1.12e-03 1.22e-03 1.33e-03
#>  [497] 1.67e-03 1.86e-03 1.95e-03 2.17e-03 2.74e-03 3.03e-03 3.30e-03 3.68e-03
#>  [505] 1.58e-02 1.73e-02 1.90e-02 2.11e-02 2.33e-02 2.62e-02 2.89e-02 3.26e-02
#>  [513] 3.95e-02 4.54e-02 5.15e-02 5.80e-02 3.06e-03 3.23e-03 3.36e-03 3.45e-03
#>  [521] 3.47e-03 3.55e-03 3.62e-03 3.69e-03 3.78e-03 3.85e-03 3.89e-03 3.93e-03
#>  [529] 3.22e-02 3.36e-02 3.57e-02 3.75e-02 3.92e-02 4.03e-02 4.12e-02 4.21e-02
#>  [537] 4.35e-02 4.44e-02 4.54e-02 4.63e-02 2.74e-04 2.84e-04 3.00e-04 3.25e-04
#>  [545] 3.62e-04 4.90e-04 5.26e-04 6.22e-04 7.02e-04 8.09e-04 9.40e-04 1.06e-03
#>  [553] 1.70e-04 2.00e-04 2.38e-04 2.88e-04 3.47e-04 4.16e-04 4.97e-04 5.98e-04
#>  [561] 7.32e-04 8.92e-04 1.06e-03 1.23e-03 5.24e-02 5.38e-02 5.59e-02 5.79e-02
#>  [569] 5.97e-02 5.92e-02 5.94e-02 5.89e-02 6.11e-02 6.21e-02 6.24e-02 6.24e-02
#>  [577] 4.19e-03 4.80e-03 5.53e-03 6.39e-03 7.05e-03 7.95e-03 8.60e-03 1.07e-02
#>  [585] 1.23e-02 1.39e-02 1.55e-02 1.73e-02 5.82e-03 6.09e-03 6.36e-03 6.56e-03
#>  [593] 6.70e-03 7.01e-03 7.38e-03 7.52e-03 7.78e-03 7.92e-03 8.00e-03 8.07e-03
#>  [601] 2.34e-03 2.72e-03 3.15e-03 3.51e-03 3.86e-03 4.28e-03 4.80e-03 5.51e-03
#>  [609] 6.39e-03 7.39e-03 8.43e-03 9.49e-03 1.97e-03 2.14e-03 2.34e-03 2.57e-03
#>  [617] 2.84e-03 3.16e-03 3.53e-03 4.24e-03 5.26e-03 6.06e-03 6.63e-03 7.50e-03
#>  [625] 3.95e-04 4.10e-04 4.31e-04 4.10e-04 4.29e-04 5.20e-04 5.81e-04 6.58e-04
#>  [633] 7.51e-04 8.60e-04 9.65e-04 1.07e-03 2.38e-03 2.61e-03 2.90e-03 3.23e-03
#>  [641] 3.52e-03 3.68e-03 3.90e-03 4.32e-03 4.75e-03 5.20e-03 5.72e-03 6.40e-03
#>  [649] 1.11e-03 1.30e-03 1.54e-03 1.85e-03 2.20e-03 2.27e-03 2.74e-03 3.27e-03
#>  [657] 3.80e-03 4.40e-03 5.02e-03 5.63e-03 1.57e-03 2.03e-03 2.46e-03 2.78e-03
#>  [665] 3.08e-03 3.43e-03 3.95e-03 4.19e-03 4.38e-03 4.88e-03 5.08e-03 5.25e-03
#>  [673] 7.16e-03 7.42e-03 7.59e-03 7.71e-03 7.84e-03 8.02e-03 8.07e-03 8.00e-03
#>  [681] 7.80e-03 7.72e-03 7.60e-03 7.50e-03 6.67e-05 7.97e-05 9.26e-05 1.05e-04
#>  [689] 1.13e-04 1.23e-04 1.32e-04 1.40e-04 1.51e-04 1.60e-04 1.73e-04 1.83e-04
#>  [697] 2.82e-01 3.10e-01 3.44e-01 3.84e-01 4.30e-01 4.81e-01 5.37e-01 5.98e-01
#>  [705] 6.61e-01 7.27e-01 7.84e-01 8.42e-01 6.22e-02 6.83e-02 7.51e-02 8.29e-02
#>  [713] 9.19e-02 1.04e-01 1.16e-01 1.28e-01 1.40e-01 1.51e-01 1.60e-01 1.69e-01
#>  [721] 1.31e-02 1.50e-02 1.73e-02 2.01e-02 2.32e-02 2.69e-02 3.26e-02 3.93e-02
#>  [729] 4.58e-02 4.80e-02 5.07e-02 5.26e-02 4.08e-03 4.69e-03 5.45e-03 6.42e-03
#>  [737] 7.58e-03 8.97e-03 1.07e-02 1.25e-02 1.35e-02 1.57e-02 1.82e-02 2.08e-02
#>  [745] 2.19e-03 2.14e-03 2.10e-03 2.15e-03 2.25e-03 2.44e-03 2.59e-03 2.64e-03
#>  [753] 2.65e-03 2.74e-03 2.90e-03 3.07e-03 1.18e-03 1.43e-03 1.71e-03 2.00e-03
#>  [761] 2.30e-03 2.61e-03 2.88e-03 3.14e-03 3.70e-03 4.15e-03 4.53e-03 4.83e-03
#>  [769] 3.61e-02 3.73e-02 3.85e-02 3.99e-02 4.12e-02 4.25e-02 4.28e-02 4.30e-02
#>  [777] 4.31e-02 4.35e-02 4.39e-02 4.41e-02 1.04e-03 1.12e-03 1.22e-03 1.37e-03
#>  [785] 1.47e-03 1.59e-03 1.70e-03 1.72e-03 1.76e-03 1.87e-03 1.98e-03 2.06e-03
#>  [793] 6.55e-02 6.94e-02 7.26e-02 7.64e-02 8.12e-02 8.63e-02 8.98e-02 9.25e-02
#>  [801] 9.42e-02 9.55e-02 9.63e-02 9.66e-02 4.16e-04 5.21e-04 6.62e-04 9.06e-04
#>  [809] 1.18e-03 1.42e-03 1.73e-03 2.09e-03 2.89e-03 3.39e-03 3.98e-03 4.55e-03
#>  [817] 4.86e-03 5.61e-03 6.54e-03 7.68e-03 9.09e-03 1.10e-02 1.33e-02 1.60e-02
#>  [825] 1.89e-02 2.14e-02 2.38e-02 2.70e-02 6.68e-03 7.09e-03 8.23e-03 9.52e-03
#>  [833] 1.12e-02 1.23e-02 1.33e-02 1.44e-02 1.57e-02 1.63e-02 1.68e-02 1.76e-02
#>  [841] 1.58e-02 1.71e-02 2.00e-02 2.28e-02 2.54e-02 2.76e-02 2.98e-02 3.15e-02
#>  [849] 3.32e-02 3.50e-02 3.63e-02 3.71e-02 7.58e-05 1.16e-04 2.26e-04 3.91e-04
#>  [857] 5.93e-04 8.19e-04 1.09e-03 1.39e-03 1.03e-03 1.29e-03 1.56e-03 1.85e-03
#>  [865] 1.05e-03 1.20e-03 1.39e-03 1.61e-03 1.99e-03 2.32e-03 2.30e-03 2.30e-03
#>  [873] 2.40e-03 2.56e-03 2.74e-03 2.93e-03 5.22e-04 5.71e-04 6.32e-04 7.10e-04
#>  [881] 8.01e-04 9.04e-04 1.03e-03 1.17e-03 1.32e-03 1.46e-03 1.51e-03 1.48e-03
#>  [889] 6.09e-04 6.95e-04 7.98e-04 9.25e-04 1.08e-03 1.25e-03 1.44e-03 1.68e-03
#>  [897] 1.41e-03 1.62e-03 2.09e-03 2.38e-03 7.28e-04 8.66e-04 1.05e-03 1.29e-03
#>  [905] 1.61e-03 2.02e-03 2.49e-03 2.84e-03 3.26e-03 3.56e-03 4.03e-03 4.53e-03
#>  [913] 3.57e-03 3.88e-03 4.28e-03 4.76e-03 5.33e-03 6.03e-03 6.91e-03 7.97e-03
#>  [921] 9.21e-03 1.07e-02 1.24e-02 1.45e-02 2.17e-03 2.40e-03 2.71e-03 3.10e-03
#>  [929] 3.54e-03 4.23e-03 4.89e-03 5.89e-03 7.55e-03 7.86e-03 8.92e-03 1.01e-02
#>  [937] 5.07e-03 5.82e-03 6.71e-03 7.66e-03 8.63e-03 9.70e-03 1.09e-02 1.23e-02
#>  [945] 1.38e-02 1.55e-02 1.71e-02 1.88e-02 2.87e-03 3.17e-03 3.51e-03 3.91e-03
#>  [953] 4.37e-03 4.88e-03 5.26e-03 5.74e-03 6.34e-03 7.07e-03 7.98e-03 9.08e-03
#>  [961] 7.30e-04 7.71e-04 8.24e-04 8.88e-04 9.65e-04 1.06e-03 1.18e-03 1.35e-03
#>  [969] 1.56e-03 1.81e-03 2.10e-03 2.43e-03 3.46e-04 4.17e-04 4.86e-04 5.53e-04
#>  [977] 6.00e-04 6.47e-04 7.07e-04 7.45e-04 7.86e-04 8.26e-04 8.65e-04 9.03e-04
#>  [985] 2.28e-02 2.65e-02 3.11e-02 3.64e-02 4.24e-02 4.83e-02 5.43e-02 6.07e-02
#>  [993] 6.68e-02 7.27e-02 7.77e-02 8.24e-02 5.62e-04 6.23e-04 7.21e-04 8.26e-04
#> [1001] 9.56e-04 1.11e-03 1.29e-03 1.48e-03 1.71e-03 1.85e-03 1.98e-03 2.13e-03
#> [1009] 2.68e-04 2.90e-04 3.14e-04 3.34e-04 3.55e-04 3.79e-04 3.81e-04 3.86e-04
#> [1017] 4.26e-04 4.80e-04 5.01e-04 4.74e-04 7.49e-03 8.60e-03 9.86e-03 1.12e-02
#> [1025] 1.26e-02 1.39e-02 1.53e-02 1.74e-02 1.95e-02 2.16e-02 2.36e-02 2.56e-02
#> [1033] 4.84e-03 5.29e-03 5.86e-03 6.54e-03 7.39e-03 8.39e-03 9.50e-03 9.73e-03
#> [1041] 9.94e-03 1.25e-02 1.40e-02 1.51e-02 1.52e-02 1.64e-02 1.79e-02 1.96e-02
#> [1049] 2.15e-02 2.39e-02 2.63e-02 2.88e-02 3.07e-02 3.28e-02 3.45e-02 3.62e-02
#> [1057] 3.23e-04 3.70e-04 4.26e-04 4.90e-04 5.78e-04 6.95e-04 7.88e-04 9.24e-04
#> [1065] 1.13e-03 1.30e-03 1.45e-03 1.51e-03 6.92e-03 7.30e-03 7.79e-03 8.49e-03
#> [1073] 9.37e-03 1.05e-02 1.19e-02 1.35e-02 1.54e-02 1.74e-02 1.96e-02 2.19e-02
#> [1081] 7.83e-03 8.32e-03 8.91e-03 9.51e-03 1.01e-02 1.05e-02 1.08e-02 1.11e-02
#> [1089] 1.15e-02 1.18e-02 1.22e-02 1.25e-02 1.47e-03 1.65e-03 1.84e-03 2.02e-03
#> [1097] 2.18e-03 2.35e-03 2.39e-03 2.47e-03 2.56e-03 2.74e-03 2.92e-03 3.08e-03
#> [1105] 8.39e-04 9.85e-04 1.16e-03 1.37e-03 1.61e-03 1.89e-03 2.21e-03 2.49e-03
#> [1113] 3.00e-03 3.45e-03 3.86e-03 4.26e-03 2.52e-03 2.75e-03 3.05e-03 3.39e-03
#> [1121] 3.79e-03 4.26e-03 4.84e-03 5.52e-03 6.32e-03 7.29e-03 8.40e-03 9.73e-03
#> [1129] 2.51e-02 2.81e-02 3.17e-02 3.58e-02 4.07e-02 4.71e-02 5.53e-02 6.18e-02
#> [1137] 7.08e-02 8.05e-02 9.09e-02 1.02e-01 2.48e-03 2.60e-03 2.71e-03 2.83e-03
#> [1145] 2.94e-03 3.02e-03 3.08e-03 3.13e-03 3.21e-03 3.30e-03 3.39e-03 3.46e-03
#> [1153] 3.40e-04 3.81e-04 4.31e-04 4.97e-04 5.83e-04 7.16e-04 9.41e-04 1.16e-03
#> [1161] 1.41e-03 1.69e-03 2.01e-03 2.38e-03 3.13e-02 3.54e-02 4.02e-02 4.59e-02
#> [1169] 5.25e-02 5.92e-02 6.93e-02 7.97e-02 9.10e-02 1.03e-01 1.16e-01 1.28e-01
#> [1177] 6.67e-04 7.61e-04 8.76e-04 1.02e-03 1.18e-03 1.35e-03 1.50e-03 1.66e-03
#> [1185] 1.84e-03 2.03e-03 2.22e-03 2.41e-03 1.13e-03 1.30e-03 1.48e-03 1.69e-03
#> [1193] 1.94e-03 2.22e-03 2.51e-03 2.90e-03 3.35e-03 3.86e-03 4.42e-03 5.01e-03
#> [1201] 6.04e-03 6.89e-03 7.93e-03 9.16e-03 1.05e-02 1.21e-02 1.37e-02 1.53e-02
#> [1209] 1.70e-02 1.87e-02 2.03e-02 2.17e-02 1.70e-02 1.97e-02 2.30e-02 2.68e-02
#> [1217] 3.09e-02 3.55e-02 4.05e-02 4.55e-02 5.09e-02 5.68e-02 6.29e-02 6.90e-02
#> [1225] 1.95e-02 2.14e-02 2.30e-02 2.41e-02 2.50e-02 2.62e-02 2.74e-02 2.86e-02
#> [1233] 2.91e-02 2.93e-02 2.92e-02 2.92e-02 6.42e-03 6.64e-03 6.79e-03 6.86e-03
#> [1241] 6.76e-03 7.28e-03 7.43e-03 7.47e-03 7.48e-03 7.66e-03 7.87e-03 8.03e-03
#> [1249] 1.64e-03 1.67e-03 1.81e-03 1.96e-03 2.11e-03 2.29e-03 2.44e-03 2.57e-03
#> [1257] 2.67e-03 2.81e-03 2.88e-03 2.94e-03 1.50e-04 1.89e-04 2.27e-04 2.68e-04
#> [1265] 3.05e-04 3.28e-04 3.47e-04 3.81e-04 4.26e-04 4.74e-04 5.19e-04 5.60e-04
#> [1273] 1.26e-02 1.35e-02 1.41e-02 1.46e-02 1.56e-02 1.64e-02 1.69e-02 1.72e-02
#> [1281] 1.72e-02 1.71e-02 1.69e-02 1.68e-02 1.88e-03 2.09e-03 2.27e-03 2.57e-03
#> [1289] 2.98e-03 3.49e-03 4.13e-03 4.77e-03 5.48e-03 5.42e-03 5.91e-03 6.67e-03
#> [1297] 0.00e+00 9.96e-07 4.05e-06 8.17e-06 1.26e-05 2.03e-05 2.93e-05 3.85e-05
#> [1305] 5.00e-05 6.49e-05 8.37e-05 1.06e-04 2.99e-03 3.31e-03 3.70e-03 4.22e-03
#> [1313] 4.86e-03 6.12e-03 8.49e-03 1.10e-02 1.28e-02 1.61e-02 1.85e-02 2.09e-02
#> [1321] 2.04e-03 2.27e-03 2.56e-03 2.96e-03 3.43e-03 3.94e-03 4.62e-03 5.39e-03
#> [1329] 6.25e-03 7.19e-03 8.20e-03 9.26e-03 5.16e-03 5.47e-03 5.73e-03 6.00e-03
#> [1337] 6.26e-03 6.54e-03 6.80e-03 6.95e-03 7.41e-03 7.79e-03 7.62e-03 7.65e-03
#> [1345] 1.58e-03 1.70e-03 1.83e-03 1.97e-03 2.14e-03 2.34e-03 2.58e-03 2.89e-03
#> [1353] 3.19e-03 3.43e-03 4.02e-03 4.61e-03 8.09e-04 1.05e-03 1.28e-03 1.45e-03
#> [1361] 1.59e-03 1.72e-03 1.97e-03 2.07e-03 2.41e-03 2.84e-03 3.14e-03 3.41e-03
#> [1369] 2.65e-03 2.87e-03 3.17e-03 3.32e-03 3.44e-03 3.62e-03 3.78e-03 3.90e-03
#> [1377] 3.98e-03 4.04e-03 4.06e-03 4.09e-03 1.08e-03 1.12e-03 1.15e-03 1.20e-03
#> [1385] 1.24e-03 1.28e-03 1.37e-03 1.43e-03 1.47e-03 1.48e-03 1.48e-03 1.48e-03
#> [1393] 1.87e-03 2.06e-03 2.29e-03 2.55e-03 2.87e-03 3.26e-03 4.37e-03 5.20e-03
#> [1401] 4.58e-03 4.99e-03 5.83e-03 6.87e-03 1.08e-02 1.22e-02 1.39e-02 1.59e-02
#> [1409] 1.81e-02 2.05e-02 2.36e-02 2.72e-02 3.03e-02 3.24e-02 3.37e-02 3.33e-02
#> [1417] 2.16e-02 2.26e-02 2.36e-02 2.49e-02 2.61e-02 2.76e-02 2.88e-02 2.94e-02
#> [1425] 2.99e-02 3.02e-02 3.04e-02 3.06e-02 6.01e-03 6.88e-03 7.86e-03 8.86e-03
#> [1433] 9.83e-03 1.07e-02 1.16e-02 1.25e-02 1.33e-02 1.41e-02 1.48e-02 1.54e-02
#> [1441] 6.40e-03 7.35e-03 8.44e-03 9.60e-03 1.10e-02 1.29e-02 1.54e-02 1.87e-02
#> [1449] 2.14e-02 2.43e-02 2.81e-02 3.20e-02 1.75e-04 2.02e-04 2.35e-04 2.74e-04
#> [1457] 3.19e-04 3.73e-04 4.47e-04 5.46e-04 6.84e-04 7.54e-04 8.12e-04 8.14e-04
#> [1465] 5.36e-03 5.54e-03 5.69e-03 5.92e-03 6.11e-03 6.21e-03 6.27e-03 6.34e-03
#> [1473] 6.57e-03 6.70e-03 6.75e-03 6.80e-03 3.61e-03 3.84e-03 4.25e-03 4.55e-03
#> [1481] 4.81e-03 4.74e-03 4.86e-03 5.00e-03 5.26e-03 5.41e-03 5.54e-03 5.68e-03
#> [1489] 2.73e-03 3.10e-03 3.62e-03 4.26e-03 5.04e-03 5.97e-03 7.09e-03 8.48e-03
#> [1497] 9.98e-03 1.14e-02 1.30e-02 1.46e-02 6.44e-03 7.66e-03 8.99e-03 1.03e-02
#> [1505] 1.15e-02 1.27e-02 1.40e-02 1.49e-02 1.56e-02 1.64e-02 1.70e-02 1.75e-02
#> [1513] 6.27e-03 7.12e-03 8.19e-03 9.52e-03 1.11e-02 1.29e-02 1.50e-02 1.74e-02
#> [1521] 2.01e-02 2.32e-02 2.62e-02 2.89e-02 1.61e-02 1.89e-02 2.21e-02 2.58e-02
#> [1529] 2.97e-02 3.34e-02 3.70e-02 4.01e-02 4.29e-02 4.56e-02 4.76e-02 4.93e-02
#> [1537] 8.79e-04 9.84e-04 1.11e-03 1.27e-03 1.51e-03 1.71e-03 1.96e-03 2.35e-03
#> [1545] 2.80e-03 3.23e-03 3.73e-03 4.28e-03 4.57e-04 5.35e-04 6.28e-04 6.83e-04
#> [1553] 6.94e-04 7.42e-04 8.01e-04 8.58e-04 8.52e-04 8.18e-04 7.90e-04 7.56e-04
#> [1561] 2.72e-03 2.95e-03 3.21e-03 3.58e-03 3.98e-03 4.51e-03 5.06e-03 5.81e-03
#> [1569] 6.42e-03 6.96e-03 7.36e-03 7.75e-03 1.68e-02 1.94e-02 2.25e-02 2.53e-02
#> [1577] 2.84e-02 3.21e-02 3.58e-02 4.01e-02 4.41e-02 4.78e-02 5.10e-02 5.39e-02
#> [1585] 4.37e-03 5.02e-03 5.79e-03 6.70e-03 7.68e-03 8.64e-03 9.77e-03 1.15e-02
#> [1593] 1.38e-02 1.60e-02 1.87e-02 2.21e-02 3.82e-02 3.90e-02 4.04e-02 4.16e-02
#> [1601] 4.25e-02 4.26e-02 4.27e-02 4.32e-02 4.38e-02 4.46e-02 4.54e-02 4.60e-02
#> [1609] 1.19e-01 1.30e-01 1.41e-01 1.51e-01 1.59e-01 1.67e-01 1.76e-01 1.84e-01
#> [1617] 1.95e-01 2.07e-01 2.18e-01 2.28e-01 1.66e-03 1.79e-03 1.93e-03 2.04e-03
#> [1625] 2.10e-03 2.13e-03 2.19e-03 2.26e-03 2.34e-03 2.43e-03 2.50e-03 2.57e-03
#> [1633] 4.08e-03 5.04e-03 6.13e-03 7.32e-03 8.69e-03 1.02e-02 1.18e-02 1.35e-02
#> [1641] 1.53e-02 1.69e-02 1.84e-02 1.97e-02 1.99e-02 2.19e-02 2.56e-02 2.99e-02
#> [1649] 3.38e-02 3.83e-02 4.25e-02 4.76e-02 5.30e-02 5.76e-02 6.13e-02 6.46e-02
#> [1657] 7.36e-04 7.66e-04 8.14e-04 8.21e-04 7.81e-04 9.11e-04 1.04e-03 1.24e-03
#> [1665] 1.55e-03 2.10e-03 2.53e-03 3.00e-03 3.72e-03 4.12e-03 4.60e-03 5.07e-03
#> [1673] 5.57e-03 6.33e-03 7.28e-03 8.46e-03 1.01e-02 1.20e-02 1.41e-02 1.68e-02
#> [1681] 1.98e-03 2.24e-03 2.55e-03 2.91e-03 3.37e-03 3.91e-03 4.58e-03 5.47e-03
#> [1689] 6.31e-03 7.10e-03 7.99e-03 8.86e-03 2.29e-03 2.72e-03 3.20e-03 3.74e-03
#> [1697] 4.40e-03 4.99e-03 5.75e-03 6.94e-03 8.07e-03 8.60e-03 9.00e-03 9.29e-03

There is still some duplication in this code. We are calulating some version of the range three times. Pulling out intermediate calculations into named variables is a good practice, because it becomes clearer what the code is doing.

x <- gap$pop

rng <- range(x)
(x - rng[1]) / (rng[2] - rng[1])
#>    [1] 6.34e-03 6.96e-03 7.74e-03 8.70e-03 9.87e-03 1.12e-02 9.72e-03 1.05e-02
#>    [9] 1.23e-02 1.68e-02 1.91e-02 2.41e-02 9.27e-04 1.07e-03 1.27e-03 1.46e-03
#>   [17] 1.67e-03 1.86e-03 2.06e-03 2.29e-03 2.48e-03 2.55e-03 2.62e-03 2.69e-03
#>   [25] 6.99e-03 7.74e-03 8.30e-03 9.63e-03 1.11e-02 1.30e-02 1.51e-02 1.76e-02
#>   [33] 1.99e-02 2.20e-02 2.37e-02 2.52e-02 3.16e-03 3.41e-03 3.61e-03 3.93e-03
#>   [41] 4.42e-03 4.63e-03 5.28e-03 5.93e-03 6.58e-03 7.44e-03 8.19e-03 9.37e-03
#>   [49] 1.35e-02 1.48e-02 1.61e-02 1.73e-02 1.87e-02 2.04e-02 2.22e-02 2.39e-02
#>   [57] 2.57e-02 2.74e-02 2.90e-02 3.05e-02 6.55e-03 7.32e-03 8.14e-03 8.96e-03
#>   [65] 9.95e-03 1.06e-02 1.15e-02 1.23e-02 1.32e-02 1.40e-02 1.48e-02 1.55e-02
#>   [73] 5.21e-03 5.24e-03 5.36e-03 5.55e-03 5.68e-03 5.69e-03 5.70e-03 5.70e-03
#>   [81] 5.96e-03 6.07e-03 6.13e-03 6.17e-03 4.58e-05 5.96e-05 8.48e-05 1.08e-04
#>   [89] 1.30e-04 1.80e-04 2.41e-04 2.99e-04 3.56e-04 4.08e-04 4.52e-04 4.92e-04
#>   [97] 3.55e-02 3.89e-02 4.31e-02 4.76e-02 5.36e-02 6.09e-02 7.05e-02 7.86e-02
#>  [105] 8.62e-02 9.35e-02 1.03e-01 1.14e-01 6.58e-03 6.77e-03 6.95e-03 7.20e-03
#>  [113] 7.32e-03 7.40e-03 7.43e-03 7.44e-03 7.57e-03 7.69e-03 7.77e-03 7.84e-03
#>  [121] 1.27e-03 1.41e-03 1.59e-03 1.80e-03 2.05e-03 2.36e-03 2.72e-03 3.17e-03
#>  [129] 3.73e-03 4.55e-03 5.28e-03 6.08e-03 2.14e-03 2.39e-03 2.68e-03 3.02e-03
#>  [137] 3.42e-03 3.81e-03 4.23e-03 4.62e-03 5.18e-03 5.79e-03 6.36e-03 6.87e-03
#>  [145] 2.07e-03 2.29e-03 2.49e-03 2.67e-03 2.85e-03 3.05e-03 3.12e-03 3.25e-03
#>  [153] 3.18e-03 2.69e-03 3.11e-03 3.41e-03 2.90e-04 3.14e-04 3.43e-04 3.74e-04
#>  [161] 4.24e-04 5.47e-04 6.90e-04 8.28e-04 9.73e-04 1.12e-03 1.19e-03 1.20e-03
#>  [169] 4.29e-02 4.97e-02 5.76e-02 6.67e-02 7.64e-02 8.66e-02 9.78e-02 1.08e-01
#>  [177] 1.18e-01 1.28e-01 1.36e-01 1.44e-01 5.47e-03 5.76e-03 6.03e-03 6.26e-03
#>  [185] 6.46e-03 6.63e-03 6.70e-03 6.76e-03 6.52e-03 6.07e-03 5.76e-03 5.51e-03
#>  [193] 3.34e-03 3.53e-03 3.69e-03 3.84e-03 4.08e-03 4.42e-03 4.99e-03 5.71e-03
#>  [201] 6.69e-03 7.81e-03 9.25e-03 1.08e-02 1.81e-03 1.98e-03 2.20e-03 2.48e-03
#>  [209] 2.63e-03 2.86e-03 3.43e-03 3.84e-03 4.36e-03 4.60e-03 5.28e-03 6.32e-03
#>  [217] 3.51e-03 3.99e-03 4.57e-03 5.23e-03 5.60e-03 5.25e-03 5.47e-03 6.30e-03
#>  [225] 7.65e-03 8.89e-03 9.76e-03 1.07e-02 3.75e-03 4.02e-03 4.35e-03 4.76e-03
#>  [233] 5.28e-03 5.99e-03 6.97e-03 8.13e-03 9.41e-03 1.07e-02 1.20e-02 1.34e-02
#>  [241] 1.12e-02 1.29e-02 1.44e-02 1.57e-02 1.69e-02 1.80e-02 1.91e-02 2.01e-02
#>  [249] 2.16e-02 2.29e-02 2.41e-02 2.53e-02 9.34e-04 1.01e-03 1.11e-03 1.27e-03
#>  [257] 1.42e-03 1.60e-03 1.83e-03 2.11e-03 2.43e-03 2.76e-03 3.02e-03 3.27e-03
#>  [265] 1.99e-03 2.15e-03 2.34e-03 2.61e-03 2.91e-03 3.28e-03 3.65e-03 4.12e-03
#>  [273] 4.83e-03 5.69e-03 6.66e-03 7.72e-03 4.79e-03 5.30e-03 5.99e-03 6.67e-03
#>  [281] 7.32e-03 7.99e-03 8.67e-03 9.41e-03 1.02e-02 1.10e-02 1.17e-02 1.23e-02
#>  [289] 4.22e-01 4.83e-01 5.05e-01 5.72e-01 6.54e-01 7.15e-01 7.59e-01 8.22e-01
#>  [297] 8.83e-01 9.33e-01 9.71e-01 1.00e+00 9.32e-03 1.09e-02 1.29e-02 1.49e-02
#>  [305] 1.71e-02 1.90e-02 2.10e-02 2.34e-02 2.59e-02 2.85e-02 3.11e-02 3.35e-02
#>  [313] 7.12e-05 8.41e-05 9.99e-05 1.19e-04 1.44e-04 1.86e-04 2.19e-04 2.54e-04
#>  [321] 2.99e-04 3.55e-04 4.20e-04 4.94e-04 1.06e-02 1.18e-02 1.32e-02 1.51e-02
#>  [329] 1.74e-02 2.00e-02 2.32e-02 2.69e-02 3.16e-02 3.62e-02 4.20e-02 4.90e-02
#>  [337] 6.03e-04 6.68e-04 7.49e-04 8.49e-04 9.71e-04 1.12e-03 1.30e-03 1.52e-03
#>  [345] 1.78e-03 2.08e-03 2.48e-03 2.84e-03 6.57e-04 7.98e-04 9.75e-04 1.16e-03
#>  [353] 1.35e-03 1.55e-03 1.79e-03 2.08e-03 2.36e-03 2.62e-03 2.86e-03 3.09e-03
#>  [361] 2.21e-03 2.46e-03 2.86e-03 3.55e-03 4.56e-03 5.61e-03 6.80e-03 8.12e-03
#>  [369] 9.64e-03 1.10e-02 1.23e-02 1.36e-02 2.90e-03 2.98e-03 3.05e-03 3.12e-03
#>  [377] 3.16e-03 3.23e-03 3.30e-03 3.36e-03 3.36e-03 3.33e-03 3.35e-03 3.36e-03
#>  [385] 4.51e-03 4.99e-03 5.46e-03 6.13e-03 6.65e-03 7.19e-03 7.38e-03 7.72e-03
#>  [393] 8.09e-03 8.28e-03 8.47e-03 8.61e-03 6.87e-03 7.17e-03 7.25e-03 7.41e-03
#>  [401] 7.43e-03 7.66e-03 7.77e-03 7.77e-03 7.78e-03 7.77e-03 7.73e-03 7.71e-03
#>  [409] 3.24e-03 3.36e-03 3.48e-03 3.62e-03 3.74e-03 3.81e-03 3.84e-03 3.84e-03
#>  [417] 3.88e-03 3.96e-03 4.03e-03 4.10e-03 2.38e-06 8.98e-06 2.27e-05 5.13e-05
#>  [425] 9.01e-05 1.28e-04 1.87e-04 1.90e-04 2.46e-04 2.71e-04 2.94e-04 3.31e-04
#>  [433] 1.84e-03 2.17e-03 2.57e-03 3.03e-03 3.50e-03 3.98e-03 4.48e-03 5.00e-03
#>  [441] 5.53e-03 6.02e-03 6.51e-03 7.02e-03 2.65e-03 3.03e-03 3.50e-03 4.07e-03
#>  [449] 4.73e-03 5.47e-03 6.30e-03 7.19e-03 8.11e-03 8.99e-03 9.75e-03 1.04e-02
#>  [457] 1.68e-02 1.89e-02 2.13e-02 2.40e-02 2.64e-02 2.94e-02 3.46e-02 4.00e-02
#>  [465] 4.50e-02 5.01e-02 5.56e-02 6.08e-02 1.50e-03 1.74e-03 2.04e-03 2.41e-03
#>  [473] 2.83e-03 3.20e-03 3.35e-03 3.63e-03 3.95e-03 4.34e-03 4.77e-03 5.22e-03
#>  [481] 1.19e-04 1.31e-04 1.43e-04 1.52e-04 1.65e-04 1.01e-04 1.71e-04 2.13e-04
#>  [489] 2.49e-04 2.88e-04 3.30e-04 3.73e-04 1.05e-03 1.12e-03 1.22e-03 1.33e-03
#>  [497] 1.67e-03 1.86e-03 1.95e-03 2.17e-03 2.74e-03 3.03e-03 3.30e-03 3.68e-03
#>  [505] 1.58e-02 1.73e-02 1.90e-02 2.11e-02 2.33e-02 2.62e-02 2.89e-02 3.26e-02
#>  [513] 3.95e-02 4.54e-02 5.15e-02 5.80e-02 3.06e-03 3.23e-03 3.36e-03 3.45e-03
#>  [521] 3.47e-03 3.55e-03 3.62e-03 3.69e-03 3.78e-03 3.85e-03 3.89e-03 3.93e-03
#>  [529] 3.22e-02 3.36e-02 3.57e-02 3.75e-02 3.92e-02 4.03e-02 4.12e-02 4.21e-02
#>  [537] 4.35e-02 4.44e-02 4.54e-02 4.63e-02 2.74e-04 2.84e-04 3.00e-04 3.25e-04
#>  [545] 3.62e-04 4.90e-04 5.26e-04 6.22e-04 7.02e-04 8.09e-04 9.40e-04 1.06e-03
#>  [553] 1.70e-04 2.00e-04 2.38e-04 2.88e-04 3.47e-04 4.16e-04 4.97e-04 5.98e-04
#>  [561] 7.32e-04 8.92e-04 1.06e-03 1.23e-03 5.24e-02 5.38e-02 5.59e-02 5.79e-02
#>  [569] 5.97e-02 5.92e-02 5.94e-02 5.89e-02 6.11e-02 6.21e-02 6.24e-02 6.24e-02
#>  [577] 4.19e-03 4.80e-03 5.53e-03 6.39e-03 7.05e-03 7.95e-03 8.60e-03 1.07e-02
#>  [585] 1.23e-02 1.39e-02 1.55e-02 1.73e-02 5.82e-03 6.09e-03 6.36e-03 6.56e-03
#>  [593] 6.70e-03 7.01e-03 7.38e-03 7.52e-03 7.78e-03 7.92e-03 8.00e-03 8.07e-03
#>  [601] 2.34e-03 2.72e-03 3.15e-03 3.51e-03 3.86e-03 4.28e-03 4.80e-03 5.51e-03
#>  [609] 6.39e-03 7.39e-03 8.43e-03 9.49e-03 1.97e-03 2.14e-03 2.34e-03 2.57e-03
#>  [617] 2.84e-03 3.16e-03 3.53e-03 4.24e-03 5.26e-03 6.06e-03 6.63e-03 7.50e-03
#>  [625] 3.95e-04 4.10e-04 4.31e-04 4.10e-04 4.29e-04 5.20e-04 5.81e-04 6.58e-04
#>  [633] 7.51e-04 8.60e-04 9.65e-04 1.07e-03 2.38e-03 2.61e-03 2.90e-03 3.23e-03
#>  [641] 3.52e-03 3.68e-03 3.90e-03 4.32e-03 4.75e-03 5.20e-03 5.72e-03 6.40e-03
#>  [649] 1.11e-03 1.30e-03 1.54e-03 1.85e-03 2.20e-03 2.27e-03 2.74e-03 3.27e-03
#>  [657] 3.80e-03 4.40e-03 5.02e-03 5.63e-03 1.57e-03 2.03e-03 2.46e-03 2.78e-03
#>  [665] 3.08e-03 3.43e-03 3.95e-03 4.19e-03 4.38e-03 4.88e-03 5.08e-03 5.25e-03
#>  [673] 7.16e-03 7.42e-03 7.59e-03 7.71e-03 7.84e-03 8.02e-03 8.07e-03 8.00e-03
#>  [681] 7.80e-03 7.72e-03 7.60e-03 7.50e-03 6.67e-05 7.97e-05 9.26e-05 1.05e-04
#>  [689] 1.13e-04 1.23e-04 1.32e-04 1.40e-04 1.51e-04 1.60e-04 1.73e-04 1.83e-04
#>  [697] 2.82e-01 3.10e-01 3.44e-01 3.84e-01 4.30e-01 4.81e-01 5.37e-01 5.98e-01
#>  [705] 6.61e-01 7.27e-01 7.84e-01 8.42e-01 6.22e-02 6.83e-02 7.51e-02 8.29e-02
#>  [713] 9.19e-02 1.04e-01 1.16e-01 1.28e-01 1.40e-01 1.51e-01 1.60e-01 1.69e-01
#>  [721] 1.31e-02 1.50e-02 1.73e-02 2.01e-02 2.32e-02 2.69e-02 3.26e-02 3.93e-02
#>  [729] 4.58e-02 4.80e-02 5.07e-02 5.26e-02 4.08e-03 4.69e-03 5.45e-03 6.42e-03
#>  [737] 7.58e-03 8.97e-03 1.07e-02 1.25e-02 1.35e-02 1.57e-02 1.82e-02 2.08e-02
#>  [745] 2.19e-03 2.14e-03 2.10e-03 2.15e-03 2.25e-03 2.44e-03 2.59e-03 2.64e-03
#>  [753] 2.65e-03 2.74e-03 2.90e-03 3.07e-03 1.18e-03 1.43e-03 1.71e-03 2.00e-03
#>  [761] 2.30e-03 2.61e-03 2.88e-03 3.14e-03 3.70e-03 4.15e-03 4.53e-03 4.83e-03
#>  [769] 3.61e-02 3.73e-02 3.85e-02 3.99e-02 4.12e-02 4.25e-02 4.28e-02 4.30e-02
#>  [777] 4.31e-02 4.35e-02 4.39e-02 4.41e-02 1.04e-03 1.12e-03 1.22e-03 1.37e-03
#>  [785] 1.47e-03 1.59e-03 1.70e-03 1.72e-03 1.76e-03 1.87e-03 1.98e-03 2.06e-03
#>  [793] 6.55e-02 6.94e-02 7.26e-02 7.64e-02 8.12e-02 8.63e-02 8.98e-02 9.25e-02
#>  [801] 9.42e-02 9.55e-02 9.63e-02 9.66e-02 4.16e-04 5.21e-04 6.62e-04 9.06e-04
#>  [809] 1.18e-03 1.42e-03 1.73e-03 2.09e-03 2.89e-03 3.39e-03 3.98e-03 4.55e-03
#>  [817] 4.86e-03 5.61e-03 6.54e-03 7.68e-03 9.09e-03 1.10e-02 1.33e-02 1.60e-02
#>  [825] 1.89e-02 2.14e-02 2.38e-02 2.70e-02 6.68e-03 7.09e-03 8.23e-03 9.52e-03
#>  [833] 1.12e-02 1.23e-02 1.33e-02 1.44e-02 1.57e-02 1.63e-02 1.68e-02 1.76e-02
#>  [841] 1.58e-02 1.71e-02 2.00e-02 2.28e-02 2.54e-02 2.76e-02 2.98e-02 3.15e-02
#>  [849] 3.32e-02 3.50e-02 3.63e-02 3.71e-02 7.58e-05 1.16e-04 2.26e-04 3.91e-04
#>  [857] 5.93e-04 8.19e-04 1.09e-03 1.39e-03 1.03e-03 1.29e-03 1.56e-03 1.85e-03
#>  [865] 1.05e-03 1.20e-03 1.39e-03 1.61e-03 1.99e-03 2.32e-03 2.30e-03 2.30e-03
#>  [873] 2.40e-03 2.56e-03 2.74e-03 2.93e-03 5.22e-04 5.71e-04 6.32e-04 7.10e-04
#>  [881] 8.01e-04 9.04e-04 1.03e-03 1.17e-03 1.32e-03 1.46e-03 1.51e-03 1.48e-03
#>  [889] 6.09e-04 6.95e-04 7.98e-04 9.25e-04 1.08e-03 1.25e-03 1.44e-03 1.68e-03
#>  [897] 1.41e-03 1.62e-03 2.09e-03 2.38e-03 7.28e-04 8.66e-04 1.05e-03 1.29e-03
#>  [905] 1.61e-03 2.02e-03 2.49e-03 2.84e-03 3.26e-03 3.56e-03 4.03e-03 4.53e-03
#>  [913] 3.57e-03 3.88e-03 4.28e-03 4.76e-03 5.33e-03 6.03e-03 6.91e-03 7.97e-03
#>  [921] 9.21e-03 1.07e-02 1.24e-02 1.45e-02 2.17e-03 2.40e-03 2.71e-03 3.10e-03
#>  [929] 3.54e-03 4.23e-03 4.89e-03 5.89e-03 7.55e-03 7.86e-03 8.92e-03 1.01e-02
#>  [937] 5.07e-03 5.82e-03 6.71e-03 7.66e-03 8.63e-03 9.70e-03 1.09e-02 1.23e-02
#>  [945] 1.38e-02 1.55e-02 1.71e-02 1.88e-02 2.87e-03 3.17e-03 3.51e-03 3.91e-03
#>  [953] 4.37e-03 4.88e-03 5.26e-03 5.74e-03 6.34e-03 7.07e-03 7.98e-03 9.08e-03
#>  [961] 7.30e-04 7.71e-04 8.24e-04 8.88e-04 9.65e-04 1.06e-03 1.18e-03 1.35e-03
#>  [969] 1.56e-03 1.81e-03 2.10e-03 2.43e-03 3.46e-04 4.17e-04 4.86e-04 5.53e-04
#>  [977] 6.00e-04 6.47e-04 7.07e-04 7.45e-04 7.86e-04 8.26e-04 8.65e-04 9.03e-04
#>  [985] 2.28e-02 2.65e-02 3.11e-02 3.64e-02 4.24e-02 4.83e-02 5.43e-02 6.07e-02
#>  [993] 6.68e-02 7.27e-02 7.77e-02 8.24e-02 5.62e-04 6.23e-04 7.21e-04 8.26e-04
#> [1001] 9.56e-04 1.11e-03 1.29e-03 1.48e-03 1.71e-03 1.85e-03 1.98e-03 2.13e-03
#> [1009] 2.68e-04 2.90e-04 3.14e-04 3.34e-04 3.55e-04 3.79e-04 3.81e-04 3.86e-04
#> [1017] 4.26e-04 4.80e-04 5.01e-04 4.74e-04 7.49e-03 8.60e-03 9.86e-03 1.12e-02
#> [1025] 1.26e-02 1.39e-02 1.53e-02 1.74e-02 1.95e-02 2.16e-02 2.36e-02 2.56e-02
#> [1033] 4.84e-03 5.29e-03 5.86e-03 6.54e-03 7.39e-03 8.39e-03 9.50e-03 9.73e-03
#> [1041] 9.94e-03 1.25e-02 1.40e-02 1.51e-02 1.52e-02 1.64e-02 1.79e-02 1.96e-02
#> [1049] 2.15e-02 2.39e-02 2.63e-02 2.88e-02 3.07e-02 3.28e-02 3.45e-02 3.62e-02
#> [1057] 3.23e-04 3.70e-04 4.26e-04 4.90e-04 5.78e-04 6.95e-04 7.88e-04 9.24e-04
#> [1065] 1.13e-03 1.30e-03 1.45e-03 1.51e-03 6.92e-03 7.30e-03 7.79e-03 8.49e-03
#> [1073] 9.37e-03 1.05e-02 1.19e-02 1.35e-02 1.54e-02 1.74e-02 1.96e-02 2.19e-02
#> [1081] 7.83e-03 8.32e-03 8.91e-03 9.51e-03 1.01e-02 1.05e-02 1.08e-02 1.11e-02
#> [1089] 1.15e-02 1.18e-02 1.22e-02 1.25e-02 1.47e-03 1.65e-03 1.84e-03 2.02e-03
#> [1097] 2.18e-03 2.35e-03 2.39e-03 2.47e-03 2.56e-03 2.74e-03 2.92e-03 3.08e-03
#> [1105] 8.39e-04 9.85e-04 1.16e-03 1.37e-03 1.61e-03 1.89e-03 2.21e-03 2.49e-03
#> [1113] 3.00e-03 3.45e-03 3.86e-03 4.26e-03 2.52e-03 2.75e-03 3.05e-03 3.39e-03
#> [1121] 3.79e-03 4.26e-03 4.84e-03 5.52e-03 6.32e-03 7.29e-03 8.40e-03 9.73e-03
#> [1129] 2.51e-02 2.81e-02 3.17e-02 3.58e-02 4.07e-02 4.71e-02 5.53e-02 6.18e-02
#> [1137] 7.08e-02 8.05e-02 9.09e-02 1.02e-01 2.48e-03 2.60e-03 2.71e-03 2.83e-03
#> [1145] 2.94e-03 3.02e-03 3.08e-03 3.13e-03 3.21e-03 3.30e-03 3.39e-03 3.46e-03
#> [1153] 3.40e-04 3.81e-04 4.31e-04 4.97e-04 5.83e-04 7.16e-04 9.41e-04 1.16e-03
#> [1161] 1.41e-03 1.69e-03 2.01e-03 2.38e-03 3.13e-02 3.54e-02 4.02e-02 4.59e-02
#> [1169] 5.25e-02 5.92e-02 6.93e-02 7.97e-02 9.10e-02 1.03e-01 1.16e-01 1.28e-01
#> [1177] 6.67e-04 7.61e-04 8.76e-04 1.02e-03 1.18e-03 1.35e-03 1.50e-03 1.66e-03
#> [1185] 1.84e-03 2.03e-03 2.22e-03 2.41e-03 1.13e-03 1.30e-03 1.48e-03 1.69e-03
#> [1193] 1.94e-03 2.22e-03 2.51e-03 2.90e-03 3.35e-03 3.86e-03 4.42e-03 5.01e-03
#> [1201] 6.04e-03 6.89e-03 7.93e-03 9.16e-03 1.05e-02 1.21e-02 1.37e-02 1.53e-02
#> [1209] 1.70e-02 1.87e-02 2.03e-02 2.17e-02 1.70e-02 1.97e-02 2.30e-02 2.68e-02
#> [1217] 3.09e-02 3.55e-02 4.05e-02 4.55e-02 5.09e-02 5.68e-02 6.29e-02 6.90e-02
#> [1225] 1.95e-02 2.14e-02 2.30e-02 2.41e-02 2.50e-02 2.62e-02 2.74e-02 2.86e-02
#> [1233] 2.91e-02 2.93e-02 2.92e-02 2.92e-02 6.42e-03 6.64e-03 6.79e-03 6.86e-03
#> [1241] 6.76e-03 7.28e-03 7.43e-03 7.47e-03 7.48e-03 7.66e-03 7.87e-03 8.03e-03
#> [1249] 1.64e-03 1.67e-03 1.81e-03 1.96e-03 2.11e-03 2.29e-03 2.44e-03 2.57e-03
#> [1257] 2.67e-03 2.81e-03 2.88e-03 2.94e-03 1.50e-04 1.89e-04 2.27e-04 2.68e-04
#> [1265] 3.05e-04 3.28e-04 3.47e-04 3.81e-04 4.26e-04 4.74e-04 5.19e-04 5.60e-04
#> [1273] 1.26e-02 1.35e-02 1.41e-02 1.46e-02 1.56e-02 1.64e-02 1.69e-02 1.72e-02
#> [1281] 1.72e-02 1.71e-02 1.69e-02 1.68e-02 1.88e-03 2.09e-03 2.27e-03 2.57e-03
#> [1289] 2.98e-03 3.49e-03 4.13e-03 4.77e-03 5.48e-03 5.42e-03 5.91e-03 6.67e-03
#> [1297] 0.00e+00 9.96e-07 4.05e-06 8.17e-06 1.26e-05 2.03e-05 2.93e-05 3.85e-05
#> [1305] 5.00e-05 6.49e-05 8.37e-05 1.06e-04 2.99e-03 3.31e-03 3.70e-03 4.22e-03
#> [1313] 4.86e-03 6.12e-03 8.49e-03 1.10e-02 1.28e-02 1.61e-02 1.85e-02 2.09e-02
#> [1321] 2.04e-03 2.27e-03 2.56e-03 2.96e-03 3.43e-03 3.94e-03 4.62e-03 5.39e-03
#> [1329] 6.25e-03 7.19e-03 8.20e-03 9.26e-03 5.16e-03 5.47e-03 5.73e-03 6.00e-03
#> [1337] 6.26e-03 6.54e-03 6.80e-03 6.95e-03 7.41e-03 7.79e-03 7.62e-03 7.65e-03
#> [1345] 1.58e-03 1.70e-03 1.83e-03 1.97e-03 2.14e-03 2.34e-03 2.58e-03 2.89e-03
#> [1353] 3.19e-03 3.43e-03 4.02e-03 4.61e-03 8.09e-04 1.05e-03 1.28e-03 1.45e-03
#> [1361] 1.59e-03 1.72e-03 1.97e-03 2.07e-03 2.41e-03 2.84e-03 3.14e-03 3.41e-03
#> [1369] 2.65e-03 2.87e-03 3.17e-03 3.32e-03 3.44e-03 3.62e-03 3.78e-03 3.90e-03
#> [1377] 3.98e-03 4.04e-03 4.06e-03 4.09e-03 1.08e-03 1.12e-03 1.15e-03 1.20e-03
#> [1385] 1.24e-03 1.28e-03 1.37e-03 1.43e-03 1.47e-03 1.48e-03 1.48e-03 1.48e-03
#> [1393] 1.87e-03 2.06e-03 2.29e-03 2.55e-03 2.87e-03 3.26e-03 4.37e-03 5.20e-03
#> [1401] 4.58e-03 4.99e-03 5.83e-03 6.87e-03 1.08e-02 1.22e-02 1.39e-02 1.59e-02
#> [1409] 1.81e-02 2.05e-02 2.36e-02 2.72e-02 3.03e-02 3.24e-02 3.37e-02 3.33e-02
#> [1417] 2.16e-02 2.26e-02 2.36e-02 2.49e-02 2.61e-02 2.76e-02 2.88e-02 2.94e-02
#> [1425] 2.99e-02 3.02e-02 3.04e-02 3.06e-02 6.01e-03 6.88e-03 7.86e-03 8.86e-03
#> [1433] 9.83e-03 1.07e-02 1.16e-02 1.25e-02 1.33e-02 1.41e-02 1.48e-02 1.54e-02
#> [1441] 6.40e-03 7.35e-03 8.44e-03 9.60e-03 1.10e-02 1.29e-02 1.54e-02 1.87e-02
#> [1449] 2.14e-02 2.43e-02 2.81e-02 3.20e-02 1.75e-04 2.02e-04 2.35e-04 2.74e-04
#> [1457] 3.19e-04 3.73e-04 4.47e-04 5.46e-04 6.84e-04 7.54e-04 8.12e-04 8.14e-04
#> [1465] 5.36e-03 5.54e-03 5.69e-03 5.92e-03 6.11e-03 6.21e-03 6.27e-03 6.34e-03
#> [1473] 6.57e-03 6.70e-03 6.75e-03 6.80e-03 3.61e-03 3.84e-03 4.25e-03 4.55e-03
#> [1481] 4.81e-03 4.74e-03 4.86e-03 5.00e-03 5.26e-03 5.41e-03 5.54e-03 5.68e-03
#> [1489] 2.73e-03 3.10e-03 3.62e-03 4.26e-03 5.04e-03 5.97e-03 7.09e-03 8.48e-03
#> [1497] 9.98e-03 1.14e-02 1.30e-02 1.46e-02 6.44e-03 7.66e-03 8.99e-03 1.03e-02
#> [1505] 1.15e-02 1.27e-02 1.40e-02 1.49e-02 1.56e-02 1.64e-02 1.70e-02 1.75e-02
#> [1513] 6.27e-03 7.12e-03 8.19e-03 9.52e-03 1.11e-02 1.29e-02 1.50e-02 1.74e-02
#> [1521] 2.01e-02 2.32e-02 2.62e-02 2.89e-02 1.61e-02 1.89e-02 2.21e-02 2.58e-02
#> [1529] 2.97e-02 3.34e-02 3.70e-02 4.01e-02 4.29e-02 4.56e-02 4.76e-02 4.93e-02
#> [1537] 8.79e-04 9.84e-04 1.11e-03 1.27e-03 1.51e-03 1.71e-03 1.96e-03 2.35e-03
#> [1545] 2.80e-03 3.23e-03 3.73e-03 4.28e-03 4.57e-04 5.35e-04 6.28e-04 6.83e-04
#> [1553] 6.94e-04 7.42e-04 8.01e-04 8.58e-04 8.52e-04 8.18e-04 7.90e-04 7.56e-04
#> [1561] 2.72e-03 2.95e-03 3.21e-03 3.58e-03 3.98e-03 4.51e-03 5.06e-03 5.81e-03
#> [1569] 6.42e-03 6.96e-03 7.36e-03 7.75e-03 1.68e-02 1.94e-02 2.25e-02 2.53e-02
#> [1577] 2.84e-02 3.21e-02 3.58e-02 4.01e-02 4.41e-02 4.78e-02 5.10e-02 5.39e-02
#> [1585] 4.37e-03 5.02e-03 5.79e-03 6.70e-03 7.68e-03 8.64e-03 9.77e-03 1.15e-02
#> [1593] 1.38e-02 1.60e-02 1.87e-02 2.21e-02 3.82e-02 3.90e-02 4.04e-02 4.16e-02
#> [1601] 4.25e-02 4.26e-02 4.27e-02 4.32e-02 4.38e-02 4.46e-02 4.54e-02 4.60e-02
#> [1609] 1.19e-01 1.30e-01 1.41e-01 1.51e-01 1.59e-01 1.67e-01 1.76e-01 1.84e-01
#> [1617] 1.95e-01 2.07e-01 2.18e-01 2.28e-01 1.66e-03 1.79e-03 1.93e-03 2.04e-03
#> [1625] 2.10e-03 2.13e-03 2.19e-03 2.26e-03 2.34e-03 2.43e-03 2.50e-03 2.57e-03
#> [1633] 4.08e-03 5.04e-03 6.13e-03 7.32e-03 8.69e-03 1.02e-02 1.18e-02 1.35e-02
#> [1641] 1.53e-02 1.69e-02 1.84e-02 1.97e-02 1.99e-02 2.19e-02 2.56e-02 2.99e-02
#> [1649] 3.38e-02 3.83e-02 4.25e-02 4.76e-02 5.30e-02 5.76e-02 6.13e-02 6.46e-02
#> [1657] 7.36e-04 7.66e-04 8.14e-04 8.21e-04 7.81e-04 9.11e-04 1.04e-03 1.24e-03
#> [1665] 1.55e-03 2.10e-03 2.53e-03 3.00e-03 3.72e-03 4.12e-03 4.60e-03 5.07e-03
#> [1673] 5.57e-03 6.33e-03 7.28e-03 8.46e-03 1.01e-02 1.20e-02 1.41e-02 1.68e-02
#> [1681] 1.98e-03 2.24e-03 2.55e-03 2.91e-03 3.37e-03 3.91e-03 4.58e-03 5.47e-03
#> [1689] 6.31e-03 7.10e-03 7.99e-03 8.86e-03 2.29e-03 2.72e-03 3.20e-03 3.74e-03
#> [1697] 4.40e-03 4.99e-03 5.75e-03 6.94e-03 8.07e-03 8.60e-03 9.00e-03 9.29e-03

Now that I have simplified the code and checked that it still works, I can turn it into a function:

rescale01 <- function(x) {
  rng <- range(x)
  scales <- (x - rng[1]) / (rng[2] - rng[1])
  return(scales)
}

Note the overall process: I only made the function after I had figured out how to make it work with a simple input. It is easier to start with working code and turn it into a function; it is harder to create a function and then try to make it work.

At this point, it is a good idea to check your function with a few different inputs:

rescale01(c(-10, 0, 10))
#> [1] 0.0 0.5 1.0

rescale01(c(1, 2, 3, 5))
#> [1] 0.00 0.25 0.50 1.00

15.2.4 Using a Function

Two important points about using (or calling) functions:

  1. Notice that when we call a function, we are passing a value into it that is assigned to the parameter we defined when writing the function. In this case, the parameter x is automatically assigned to c(-10, 0, 10).

  2. When using functions, by default the returned object is merely printed to the screen. If you want it saved, you need to assign it to an object.

Let's see if we can simplify the original example with our brand new function:

rescale01 <- function(x) {
  rng <- range(x)
  scales <- (x - rng[1]) / (rng[2] - rng[1])
  return(scales)
}

gap_norm <- gap %>%
  mutate(pop_norm = rescale01(pop),
         gdp_norm = rescale01(gdpPercap),
         life_norm = rescale01(lifeExp))

Compared to the original, this code is easier to understand, and we have eliminated one class of copy-and-paste errors. There is still quite a bit of duplication, since we are doing the same thing to multiple columns. We will learn how to eliminate that duplication in the lesson on iteration.

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 NA values, and rescale01() fails:

rescale01(c(1, 2, NA, 3, 4, 5))
#> [1] NA NA NA NA NA NA

Because we have extracted the code into a function, we only need to make the fix in one place:

rescale01 <- function(x) {
  rng <- range(x, na.rm = T)
  scales <- (x - rng[1]) / (rng[2] - rng[1])
  return(scales)
}

rescale01(c(1, 2, NA, 3, 4, 5))
#> [1] 0.00 0.25   NA 0.50 0.75 1.00

15.2.5 Challenges

Challenge 1.

Write a function that calculates the sum of the squared value of two numbers. For instance, it should generate the following output:

my_function(3, 4)
# [1] 25

Challenge 2.

Write both_na(), a function that takes two vectors and returns the total number of NAs in both vectors.

For instance, it should generate the following output:

vec1 <- c(NA, 4, 6, 2, NA, 5, NA)
vec2 <- c(NA, "Dec", "Apr", NA, "Jul", "Apr")

my_other_function(vec1, vec2)
# [1] 5

# Hints
is.na(vec1)
sum(c(T, F))

Challenge 3.

Fill in the blanks to create a function that takes a name like "Rochelle Terman" and returns that name in uppercase and reversed, like "TERMAN, ROCHELLE".

standard_names <- function(name){
    upper_case = toupper(____) # Make upper
    upper_case_vec = strsplit(_____, split = ' ')[[1]] # Turn into a vector
    first_name = ______ # Take first name
    last_name = _______ # Take last name
    reversed_name = paste(______, _______, sep = ", ") # Reverse and separate by a comma and space
    return(reversed_name)
}

Challenge 4.

Look at the following function:

print_date <- function(year, month, day){
    joined = paste(as.character(year), as.character(month), as.character(day), sep = "/")
    return(joined)
}

Why do the two lines of code below return different values?

print_date(day=1, month=2, year=2003)
print_date(1, 2, 2003)

15.3 Iteration

In the last unit, we talked about how important it is to reduce duplication in your code by creating functions instead of copying-and-pasting. Avoiding duplication allows for more readable, more flexible, and less error-prone code.

Functions are one method of reducing duplication in your code. Another tool for reducing duplication is iteration, which lets you do the same task to multiple inputs.

In this chapter, you will learn about three approaches to iteratation:

  1. Vectorized functions.
  2. map and functional programming.
  3. Scoped verbs in dplyr.

15.3.1 Vectorized Functions

Most of R’s built-in functions are vectorized, meaning that the function will operate on all elements of a vector without needing to loop through and act on each element at a time.

That means you should never need to perform explicit iteration when performing simple mathematical computations.

x <- 1:4
x * 2
#> [1] 2 4 6 8

Notice that the multiplication happened to each element of the vector. Most built-in functions also operate element-wise on vectors:

x <- 1:4
log(x)
#> [1] 0.000 0.693 1.099 1.386

We can also add two vectors together:

x <- 1:4
y <- 6:9
x + y
#> [1]  7  9 11 13

Notice that each element of x was added to its corresponding element of y:

x:  1  2  3  4
    +  +  +  +
y:  6  7  8  9
---------------
    7  9 11 13

What happens if you add two vectors of different lengths?

1:10 + 1:2
#>  [1]  2  4  4  6  6  8  8 10 10 12

Here, R will expand the shortest vector to the same length as the longest. This is called recycling. This usually (but not always) happens silently, meaning R will not warn you. Beware!

15.3.2 Functional Programming and map

You might have used for loops in other languages. Loops are not as important in R as they are in other languages, because R is a functional programming language. This means that it is possible to wrap up for loops in a function and call that function instead of using the for loop directly.

The pattern of looping over a vector, doing something to each element, and saving the results is so common that the purrr package (part of tidyverse) provides a family of functions to do it for you. They effectively eliminate the need for many common for loops.

library(tidyverse)

There is one function for each type of output:

  1. map() makes a list.
  2. map_lgl() makes a logical vector.
  3. map_int() makes an integer vector.
  4. map_dbl() makes a double vector.
  5. map_chr() makes a character vector.

Each function takes a vector as input, applies a function to each piece, and then returns a new vector that is the same length (and has the same names) as the input.

NB: Some people will tell you to avoid for loops because they are slow. They are wrong! (Well, at least they are rather out of date, as for loops have not been slow for many years). The main benefit of using functions like map() is not speed, but clarity: They make your code easier to write and to read.

To see how map works, consider this simple data frame:

df <- data.frame(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)

What if we wanted to calculate the mean, median, and standard deviation of each column?

map_dbl(df, mean)
#>      a      b      c      d 
#> -0.441 -0.179 -0.124  0.152
map_dbl(df, median)
#>       a       b       c       d 
#> -0.2458 -0.2873 -0.0567  0.1443
map_dbl(df, sd)
#>     a     b     c     d 
#> 1.118 1.176 1.047 0.964

The data can even be piped!

df %>% map_dbl(mean)
#>      a      b      c      d 
#> -0.441 -0.179 -0.124  0.152
df %>% map_dbl(median)
#>       a       b       c       d 
#> -0.2458 -0.2873 -0.0567  0.1443
df %>% map_dbl(sd)
#>     a     b     c     d 
#> 1.118 1.176 1.047 0.964

We can also pass additional arguments. For example, the function mean passes an optional argument trim. From the help file: "The fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed."

map_dbl(df, mean, trim = 0.5)
#>       a       b       c       d 
#> -0.2458 -0.2873 -0.0567  0.1443

Check out other fun applications of map functions here.

15.3.3 Challenges

Write code that uses one of the map functions to:

Challenge 1.

Calculate the arithmetic mean for every column in mtcars.

Challenge 2.

Calculate the number of unique values in each column of iris.

Challenge 3.

Generate 10 random normals for each of \(\mu = -10\), \(0\), \(10\), and \(100\).

15.3.4 Scoped Verbs

The last iteration technique we will discuss is scoped verbs in dplyr.

Frequently, when working with dataframes, we want to apply a function to multiple columns. For example, let's say we want to calculate the mean value of each column in mtcars.

If we wanted to calculate the average of a single column, it would be pretty simple using just regular dplyr functions:

mtcars %>%
  summarize(mpg = mean(mpg))
#>    mpg
#> 1 20.1

But if we want to calculate the mean for all of them, we would have to duplicate this code many times over:

mtcars %>%
  summarize(mpg = mean(mpg),
            cyl = mean(cyl),
            disp = mean(disp),
            hp = mean(hp),
            drat = mean(drat),
            wt = mean(wt),
            qsec = mean(qsec),
            vs = mean(vs),
            am = mean(am),
            gear = mean(gear),
            carb = mean(carb))
#>    mpg  cyl disp  hp drat   wt qsec    vs    am gear carb
#> 1 20.1 6.19  231 147  3.6 3.22 17.8 0.438 0.406 3.69 2.81

This is very repetitive and prone to mistakes!

We just saw one approach to solve this problem: map. Another approach is scoped verbs.

Scoped verbs allow you to use standard verbs (or functions) in dplyr that affect multiple variables at once.

  • _if allows you to pick variables based on a predicate function like is.numeric() or is.character().
  • _at allows you to pick variables using the same syntax as select().
  • _all operates on all variables.

These verbs can apply to summarize, filter, or mutate. Let's go over summarize:

summarize_all()

summarize_all() takes a dataframe and a function and applies that function to each column.

mtcars %>%
  summarize_all(.funs = mean)
#>    mpg  cyl disp  hp drat   wt qsec    vs    am gear carb
#> 1 20.1 6.19  231 147  3.6 3.22 17.8 0.438 0.406 3.69 2.81

summarize_at()

summarize_at() allows you to pick columns in the same way as select(), that is, based on their names. There is one small difference: You need to wrap the complete selection with the vars() helper (this avoids ambiguity).

mtcars %>%
  summarize_at(.vars = vars(mpg, wt), .funs = mean)
#>    mpg   wt
#> 1 20.1 3.22

summarize_if()

summarize_if() allows you to pick variables to summarize based on some property of the column. For example, what if we want to apply a numeric summary function only to numeric columns?

iris %>%
  summarize_if(.predicate = is.numeric, .funs = mean)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width
#> 1         5.84        3.06         3.76         1.2

mutate and filter work in a similar way. To see more, check out Scoped verbs by the Data Challenge Lab.

Acknowledgments

A good portion of this lesson is based on: