Functions
December 20 2022
functions.Rmd
Just as loops allow you to repeat the same code chunk again and again in one place, functions allow you to repeat the same task multiple times, but potentially in multiple places on different data. If you have a complex piece of code that does a single discrete thing, then you may want to wrap it up into a function so that you can refer to it by a simple name and keep the script that calls it clear and easy to read.
To quote from the introduction to R4DS on functions:
One of the best ways to improve your reach as a data scientist is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy-and-pasting. Writing a function has three big advantages over using copy-and-paste:
You can give a function an evocative name that makes your code easier to understand.
As requirements change, you only need to update code in one place, instead of many.
You eliminate the chance of making incidental mistakes when you copy and paste (i.e. updating a variable name in one place, but not in another).
Functions allow us to wrap up a common task into a single line of code, irrespective of how complex and long the code chunk is, making our scripts cleaner and easier to read. They allow us to describe what the code is doing (by assigning a name to the function), making our scripts easier to understand. Finally, writing custom functions rather than duplicating code means that there is only a single place that needs to be checked for correctness, rather than having to remember all of the places your duplicated (likely copied and pasted) code exists.
Functions are really easy to write:
add_up <- function(first, second) {
first + second
}
# Add call the function to add 3 to 5!
add_up(3, 5)
#> [1] 8
The critical thing to know is that a function takes its arguments
(here the variables first
and second
) and does
something to them to calculate some result. The result that is output by
the function is the last line of code is that is run.
If you are writing a simple function, this will be the final line
before the close curly bracket (}
), here
squared
.
add_and_square <- function(first, second) {
# Calculate the result
added <- first + second
squared <- added * added
# And return it
squared
}
# And call the function to add 3 to 5 and square it!
add_and_square(3, 5)
#> [1] 64
We introduced functions in Lecture 2c and Practical A-1, and then again in Lecture 5b. They were used starting in Practical 1-2.
Functions are covered in R4DS here and R Coder covers them in some depth here. Advanced R covers functions in enormous detail starting here with an introduction to functional programming, and continuing for several chapters.