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)
}
is_evennumHere are some examples invoking this function:
is_even(1) # FALSE
is_even(12) # TRUE
is_even(999) # FALSE
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)
}
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.
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
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
No subscriptions, no auto-renewals.
Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.
Unlocking gets you access to the notes for this video plus all 200+ guides on this site.
Your support is appreciated. Thank you!