Video Notes
In R, we learn about all sorts of built-in functions such as mean, sd, sum, etc.
Oftentimes, it can be useful to define your own custom functions when there’s some calculation or action you need to execute repeatedly in your code.
To familiarize ourself with the syntax of a function, here’s a simple example:
is_even <- function(num) {
return(num %% 2 == 0)
}
- The first line assigns a new function to an object named
is_even
- In parenthesis, we define any parameters this function can accept - in this case, we defined a single parameter,
num
- The curly braces {} contain the code that will run when the function is called; in this case there is just a single line of code, but there could be many.
- The return statement is used to return a value from our function.
Here are some examples invoking this function:
is_even(1) # FALSE
is_even(12) # TRUE
is_even(999) # FALSE
Functions with no returns
Not every function has to explicitly return a value. For example, the following function can be used to save a data file and output a confirmation message to the console. In this case, the function has a side effect (the file being saved and the confirmation message), but no value is explicitly returned.
save_to_cwd <- function(data, filename) {
filepath <- file.path(getwd(), filename)
write.csv(data, filepath, row.names = FALSE)
message("File saved to: ", filepath)
}
Implicit returns
In R, if a function does not include an explicit return() statement, R automatically returns the value of the last evaluated expression.
Example:
add <- function(x, y) {
x + y # no 'return()' here
}
add(3, 5) # 8
While this behavior works fine, it’s often clearer (especially in longer or more complex functions) to include an explicit return() statement when a specific return value is expected.
Default arguments for parameters
When definining parameters you can indicate default values. For example, the following function specifies num = 1 meaning that if the function is called without providing a value for num, it will default to 1.
is_even <- function(num = 1) {
return(num %% 2 == 0)
}
# Invoking without an argument uses the default num = 1
is_even() # FALSE, because 1 is not even
Organize commonly used functions
It can be helpful to organize commonly used functions into their own scripts file that can then be included in other scripts and used as needed.
For example, if we moved our is_even function to a separate script called utils.R, we could then include it in our original script using the source method:
source('utils.R')
is_even(13) # FALSE