This page contains extra R content not covered in the demonstrations and could be considered supplementary to the module. This content is useful for completing the advanced exercises from Week 8 and focuses on creating custom functions in R.
Throughout our time with R, we have used many functions from different packages. Functions in these packages were written by other R users. While some of these functions do some complex things, every function in R is written the same way.
To create a function, you will use a function called
function()
, which has a couple of unique properties. Let’s
demonstrate how function()
works via an example. Here we
create a simple function called add_numbers that adds two numbers
together.
add_numbers <- function(number1,number2){
result <- number1 + number2
return(result)
}
And here is our new custom function in action:
add_numbers(number1 = 3, number2 = 4)
## [1] 7
Let’s break these lines of code down. function()
provides the base mechanism for defining a new function in R. Any new
function can be saved as an object using the <-
symbols,
just like anything else in R (in the example above, the object name is
add_numbers
). The object name becomes the name of the
function, which you will call to use the function. Any arguments you
want in your custom functions are named as arguments within the brackets
(in the above case, the arguments are number1
and
number2
). function()
must always be followed
by a set of curly brackets {} which can span multiple lines. Whatever
occurs within these curly brackets is performed whenever you call the
function. Each function can only return one output. This output must be
put inside a return()
function. Whatever is in the
return()
function is what is printed (or saved) when you
run your custom function. If your function does not have this command,
your function may not do anything.
Importantly, whatever happens inside the function happens in it’s own
environment/workspace. That is, any objects created inside a function
does not get saved in the global workspace. So in the example above, we
cannot call upon the object result
that we made in the
function above. If you try, it will produce an error. You can also not
use any objects in the global workspace unless it is supplied as an
argument in the function.
Creating custom functions are helpful if you are planning to do the same computation multiple times on different objects. Instead of typing out the code each time, you can write it once in a function, and use that function to perform the operation.
Something that we have to perform often is standardising variables
(or a vector of numbers). Previously we have used the
scale()
function to do this, but there are some quirks to
this function that sometimes prevents the results from being used in
future operations. Therefore, we can create a custom function that does
the whole process for us.
Here is what the code would look like if we were to standardise a vector manually:
#This is the vector we wish to standardise.
vector
vector.mean <- mean(vector,na.rm = TRUE)
vector.sd <- sd(vector,na.rm = TRUE)
z.vector <- (vector - vector.mean)/vector.sd
However, we can put this code within the curly brackets of a custom
function, and create a new function called z
. Remember to
use the return()
call to tell R that you want the
standardised vector to be the output of your new function:
z <- function(vector){
vector
vector.mean <- mean(vector,na.rm = TRUE)
vector.sd <- sd(vector,na.rm = TRUE)
z.vector <- (vector - vector.mean)/vector.sd
return(z.vector)
}
We can now use this function to standardise any vector of numbers we
come across without using the awkward c(scale())
functions.
If you would like more content on using custom functions, see the extra content page on simulations.
If you would like to practice the skills on this page, weekly exercise questions on this content are available in the advanced exercises for Week 8. You can download the interactive exercises by clicking the link below.