<- function(input_1, input_2){
function_name <- input_1 + input_2
output_value return(output_value)
}
Functions (reference card)
Function Basics
This just defines the function. To run the function you need to call it:
function_name(3, 4)
Default Arguments
Adding an =
and a value after the argument will set a default for that argument.
<- function(input_1, input_2 = 2){
function_name <- input_1 + input_2
output_value return(output_value)
}
You can can call functions without specifying arguments when there is a default and the default will be used.
function_name(3)
Or you can override the default by specifying the argument.
function_name(3, input_2 = 5)
Named vs. Unnamed arguments
When calling the function arguments are assigned based on position unless they are named. Typically we don’t name required arguments, but do name optional arguments because it lets us change only the optional arguments we want to change.
<- function(input_1, input_2 = 2, input_3 = 5){
function_name <- input_1 + input_2 + input_3
output_value return(output_value)
}
function_name(3, input_3 = 7)
Combining Functions
Intermediate Variables
<- function_1(input)
function_1_output <- function_2(function_1_output) function_2_output
Pipes
<- input |>
function_1_output function_1() |>
function_2()
Tidyverse Functions
We need to “embrace” column names when passed as arguments to functions for use in tidyverse functions.
<- function(df, filter_column, filter_value){
function_name <- filter(df, {{ filter_column }} == filter_value)
filtered_df
return_filtered_df
}
function_name(df, col_name, "A")