Functions (reference card)

Function Basics

function_name <- function(input_1, input_2){
  output_value <- input_1 + input_2
  return(output_value)
}

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_name <- function(input_1, input_2 = 2){
  output_value <- input_1 + input_2
  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_name <- function(input_1, input_2 = 2, input_3 = 5){
  output_value <- input_1 + input_2 + input_3
  return(output_value)
}
function_name(3, input_3 = 7)

Combining Functions

Intermediate Variables

function_1_output <- function_1(input)
function_2_output <- function_2(function_1_output)

Pipes

function_1_output <- input |>
  function_1() |>
  function_2()

Tidyverse Functions

We need to “embrace” column names when passed as arguments to functions for use in tidyverse functions.

function_name <- function(df, filter_column, filter_value){
  filtered_df <- filter(df, {{ filter_column }} == filter_value)
  return_filtered_df
}

function_name(df, col_name, "A")