7.2 Functions in R

Functions are your loyal servants, waiting patiently to do your bidding to the best of their ability. They’re made with the utmost care and attention … though sometimes may end up being something of a Frankenstein’s monster - with an extra limb or two and a head put on backwards. But no matter how ugly they may be they’re completely faithful to you.

They’re also very stupid.

If we asked you to go to the supermarket to get us some ingredients to make Francesinha, even if you don’t know what the heck that is, you’d be able to guess and bring at least something back. Or you could decide to make something else. Or you could ask a celebrity chef for help. Or you could pull out your phone and search online for what Francesinha is. The point is, even if we didn’t give you enough information to do the task, you’re intelligent enough to, at the very least, try to find a work around.

If instead, we asked our loyal function to do the same, it would listen intently to our request, stand still for a few milliseconds, compose itself, and then start shouting Error: 'data' must be a data frame, or other object .... It would then repeat this every single time we asked it to do the job. The point here, is that code and functions are not intelligent. They cannot find workarounds. It’s totally reliant on you, to tell it very explicitly what it needs to do step by step.

Remember two things: the intelligence of code comes from the coder, not the computer and functions need exact instructions to work.

To prevent functions from being too stupid you must provide the information the function needs in order for it to function. As with the Francesinha example, if we’d supplied a recipe list to the function, it would have managed just fine. We call this “fulfilling an argument”. The vast majority of functions require the user to fulfill at least one argument.

This can be illustrated in the pseudocode below. When we make a function we can specify what arguments the user must fulfill (e.g. argument1 and argument2), as well as what to do once it has this information (expression):

nameOfFunction <- function(argument1, argument2, ...) {expression}

The first thing to note is that we’ve used the function function() to create a new function called nameOfFunction. To walk through the above code; we’re creating a function called nameOfFunction. Within the round brackets we specify what information (i.e. arguments) the function requires to run (as many or as few as needed). These arguments are then passed to the expression part of the function. The expression can be any valid R command or set of R commands and is usually contained between a pair of braces { } (if a function is only one line long you can omit the braces). Once you run the above code, you can then use your new function by typing:

nameOfFunction(argument1, argument2)

Confused? Let’s work through an example to help clear things up.

First we are going to create a data frame called city, where columns porto, aberdeen, nairobi, and genoa are filled with 100 random values drawn from a bag (using the rnorm() function to draw random values from a Normal distribution with mean 0 and standard deviation of 1). We also include a “problem”, for us to solve later, by including 10 NA values within the nairobi column (using rep(NA, 10)).

city <- data.frame(
  porto = rnorm(100),
  aberdeen = rnorm(100),
  nairobi = c(rep(NA, 10), rnorm(90)),
  genoa = rnorm(100)
)

Let’s say that you want to multiply the values in the variables Porto and Aberdeen and create a new object called porto_aberdeen. We can do this “by hand” using:

porto_aberdeen <- city$porto * city$aberdeen

We’ve now created an object called porto_aberdeen by multiplying the vectors city$porto and city$aberdeen. Simple. If this was all we needed to do, we can stop here. R works with vectors, so doing these kinds of operations in R is actually much simpler than other programming languages, where this type of code might require loops (we say that R is a vectorised language). Something to keep in mind for later is that doing these kinds of operations with loops can be much slower compared to vectorisation.

But what if we want to repeat this multiplication many times? Let’s say we wanted to multiply columns porto and aberdeen, aberdeen and genoa, and nairobi and genoa. In this case we could copy and paste the code, replacing the relevant information.

porto_aberdeen <- city$porto * city$aberdeen
aberdeen_genoa <- city$aberdeen * city$aberdeen
nairobi_genoa <- city$nairobi * city$genoa

While this approach works, it’s easy to make mistakes. In fact, here we’ve “forgotten” to change aberdeen to genoa in the second line of code when copying and pasting. This is where writing a function comes in handy. If we were to write this as a function, there is only one source of potential error (within the function itself) instead of many copy-pasted lines of code (which we also cut down on by using a function).

In this case, we’re using some fairly trivial code where it’s maybe hard to make a genuine mistake. But what if we increased the complexity?

city$porto * city$aberdeen / city$porto + (city$porto * 10^(city$aberdeen)) 
                  - city$aberdeen - (city$porto * sqrt(city$aberdeen + 10))

Now imagine having to copy and paste this three times, and in each case having to change the porto and aberdeen variables (especially if we had to do it more than three times).

What we could do instead is generalise our code for x and y columns instead of naming specific cities. If we did this, we could recycle the x * y code. Whenever we wanted to multiple columns together, we assign a city to either x or y. We’ll assign the multiplication to the objects porto_aberdeen and aberdeen_nairobi so we can come back to them later.

# Assign x and y values
x <- city$porto
y <- city$aberdeen

# Use multiplication code
porto_aberdeen <- x * y

# Assign new x and y values
x <- city$aberdeen
y <- city$nairobi

# Reuse multiplication code
aberdeen_nairobi <- x * y

This is essentially what a function does. OK down to business, let’s call our new function multiply_columns() and define it with two arguments, x and y. In the function code we simply return the value of x * y using the return() function. Using the return() function is not strictly necessary in this example as R will automatically return the value of the last line of code in our function. We include it here to make this explicit.

multiply_columns <- function(x, y) {
  return(x * y)
}

No that we’ve defined our function we can use it. Let’s use the function to multiple the columns city$porto and city$aberdeen and assign the result to a new object called porto_aberdeen_func

porto_aberdeen_func <- multiply_columns(x = city$porto, y = city$aberdeen)
porto_aberdeen_func
##   [1]  1.295804e-01  1.260428e+00 -1.121206e-01  7.622875e-01  1.140550e+00
##   [6]  9.356345e-02  9.745476e-02 -4.618312e-01 -7.760363e-01  3.111551e-01
##  [11]  6.293673e-01  1.007670e+00 -5.671685e-02  2.409880e+00 -7.399202e-01
##  [16]  6.413649e-01  3.773841e+00  4.112661e-01 -5.691052e-01 -7.110703e-01
##  [21] -1.835657e-03 -1.235430e+00  6.803459e-01 -8.836207e-01  6.930520e-01
##  [26] -2.748012e-01  5.855237e-01  1.420832e+00  1.034637e+00  8.510149e-02
##  [31] -7.383172e-03 -1.143306e+00 -1.795621e-01  3.567536e-01  1.736984e-01
##  [36]  3.335574e-01  2.367326e+00  3.823076e-01  3.699310e-02 -1.976794e-02
##  [41] -5.696597e-01  1.004565e-02 -1.987454e-02  2.167771e+00  2.444197e-01
##  [46]  1.460125e-02 -8.029804e-01 -1.553481e-01  1.049603e+00 -3.970424e-01
##  [51] -6.673996e-01 -1.222833e+00 -4.701078e-02 -5.968147e-01 -5.074544e-01
##  [56]  5.111315e-01 -2.135874e-01  6.011958e-01 -5.806352e-05 -1.300365e-01
##  [61]  4.531212e-01  3.500490e-01 -4.425651e+00  1.826905e-02  1.526228e+00
##  [66] -2.551322e-01  2.714572e-02  4.742367e-01  1.870387e-03 -3.449777e-01
##  [71] -6.707620e-01  8.505652e-01 -7.445536e-01  2.799137e+00 -5.928118e-01
##  [76] -2.700141e-02 -6.487311e-01 -1.219498e-01 -5.384152e-01 -9.421539e-01
##  [81] -4.633844e-01  1.122476e+00  7.867460e-03 -1.065055e-01 -3.639254e-01
##  [86]  9.612756e-01  1.443977e-01 -6.754873e-02 -1.806358e-01 -1.972135e-01
##  [91] -3.515505e-01 -3.830780e-03 -1.484399e-01 -6.300445e-01  1.009896e+00
##  [96]  3.147785e-01  1.769355e+00 -1.214240e-01 -4.114780e-01 -4.824783e-01

If we’re only interested in multiplying city$porto and city$aberdeen, it would be overkill to create a function to do something once. However, the benefit of creating a function is that we now have that function added to our environment which we can use as often as we like. We also have the code to create the function, meaning we can use it in completely new projects, reducing the amount of code that has to be written (and retested) from scratch each time. As a rule of thumb, you should consider writing a function whenever you’ve copied and pasted a block of code more than twice.

To satisfy ourselves that the function has worked properly, we can compare the porto_aberdeen variable with our new variable porto_aberdeen_func using the identical() function. The identical() function tests whether two objects are exactly identical and returns either a TRUE or FALSE value. Use ?identical if you want to know more about this function.

identical(porto_aberdeen, porto_aberdeen_func)
## [1] TRUE

And we confirm that the function has produced the same result as when we do the calculation manually. We recommend getting into a habit of checking that the function you’ve created works the way you think it has.

Now let’s use our multiply_columns() function to multiply columns aberdeen and nairobi. Notice now that argument x is given the value city$aberdeenand y the value city$nairobi.

aberdeen_nairobi_func <- multiply_columns(x = city$aberdeen, y = city$nairobi)
aberdeen_nairobi_func
##   [1]            NA            NA            NA            NA            NA
##   [6]            NA            NA            NA            NA            NA
##  [11] -2.3911973504  1.6325804547 -0.0834736856 -0.1454507753  0.1724891935
##  [16] -0.6510793416 -0.5138880400 -0.0040235609 -3.1257033399 -0.0373710642
##  [21] -0.0007366709  0.1521640668  0.8465025811  2.5816555679 -0.2058160200
##  [26] -0.6044955729  0.9145064888  0.4760344232 -0.0398411378  0.1523182005
##  [31] -0.0085047613  0.6720120035  1.2424104451  0.2341893391  0.5834168489
##  [36]  0.4381442570 -1.1833746131  0.1218074181  0.0785785969 -0.0686204458
##  [41]  0.2981186421 -0.2120518325  0.2307739103 -2.1009413912 -0.9871769000
##  [46] -0.6484015937 -1.1079299304  0.2100750491  0.4758895060  0.7064327212
##  [51]  0.2210363511 -0.2483423554  0.0146607039  0.3956718435  0.3924074250
##  [56] -0.5750314490  0.1162058809  0.4177386240  0.0306458941 -1.4224736341
##  [61]  0.5513201457 -0.6061782910 -1.0308486629 -0.0061200739 -1.1344765848
##  [66]  0.0946841283  0.0310331163 -0.1657009187  0.0372138421  0.3651513122
##  [71]  0.6941466344  2.1365507953 -3.2656131236 -2.3805252356 -0.4858806831
##  [76] -0.0997362848 -0.6129841847  0.6065102359 -1.9318129640 -0.6897167840
##  [81] -2.8617656065 -0.1779454934 -0.5080842210 -0.3227129995 -0.0946008485
##  [86]  3.0175324404  0.1969679270  0.2848062661  0.4314953684 -2.2981075286
##  [91] -0.0998512296  0.0674778967  0.2534415611 -0.6993243885  0.5880596615
##  [96] -0.0395415972 -0.7053237598 -0.0598981520  0.2178585827 -0.8800205633

So far so good. All we’ve really done is wrapped the code x * y into a function, where we ask the user to specify what their x and y variables are.

Now let’s add a little complexity. If you look at the output of nairobi_genoa some of the calculations have produced NA values. This is because of those NA values we included in nairobi when we created the city data frame. Despite these NA values, the function appeared to have worked but it gave us no indication that there might be a problem. In such cases we may prefer if it had warned us that something was wrong. How can we get the function to let us know when NA values are produced? Here’s one way.

multiply_columns <- function(x, y) {
  temp_var <- x * y
  if (any(is.na(temp_var))) {
    warning("The function has produced NAs")
    return(temp_var)
  } else {
    return(temp_var)
  }
}

aberdeen_nairobi_func <- multiply_columns(city$aberdeen, city$nairobi)
## Warning in multiply_columns(city$aberdeen, city$nairobi): The function has
## produced NAs
porto_aberdeen_func <- multiply_columns(city$porto, city$aberdeen)

The core of our function is still the same. We still have x * y, but we’ve now got an extra six lines of code. Namely, we’ve included some conditional statements, if and else, to test whether any NAs have been produced and if they have we display a warning message to the user. The next section of this Chapter will explain how these work and how to use them.