How do you write a function named function_name that takes two numbers as inputs and returns the sum of those numbers?
function_name <- function(input_1, input_2){
output_value <- input_1 + input_2
return(output_value)
}
Flip the card by pressing the q key.
What do you use to send a value from a function back to the main program?
return()
return(output_value)
You write and run the following code but don’t get any output.
function_name <- function(input_1, input_2) {
output_value <- input_1 + input_2
return(output_value)
}
Why didn’t you get any output and what do you need to do to get it?
Creating a function only defines it. To run it you need to call it.
function_name(3, 5)
How do you give input_2 a default value of 2?
Add = and the value after the argument name when defining the function.
function_name <- function(input_1, input_2 = 2){
output_value <- input_1 + input_2
return(output_value)
}
function_name <- function(input_1, input_2 = 2){
output_value <- input_1 + input_2
return(output_value)
}
What does function_name(3) return?
5
input_2 wasn’t given a value, so the default of 2 is used.
function_name <- function(input_1, input_2 = 2){
output_value <- input_1 + input_2
return(output_value)
}
How do you call this function with input_1 as 3 and input_2 as 5 instead of the default?
Specify the argument to override the default.
function_name(3, input_2 = 5)
When you call a function without naming the arguments, how does R decide which value goes to which argument?
By position
The first value goes to the first argument, the second value to the second argument, etc.
What arguments should you typically name when calling a function, and why?
Optional arguments (i.e., those with defaults)
Naming them lets us change only the optional arguments we want to change.
Required arguments are typically not named.
How do you run function_1() on input and then run function_2() on the result using intermediate variables?
function_1_output <- function_1(input)
function_2_output <- function_2(function_1_output)
How do you run function_1() on input and then run function_2() on the result using pipes?
function_1_output <- input |>
function_1() |>
function_2()
What do you need to do to a column name that is passed as an argument to your function so that it works with tidyverse functions like filter()?
“Embrace” it with double curly braces
{{ column_name }}
df is short for Data Frame and represents a data table
How do you write a function that filters df to the rows where filter_column is equal to filter_value?
function_name <- function(df, filter_column, filter_value){
filtered_df <- filter(df, {{ filter_column }} == filter_value)
return(filtered_df)
}
function_name <- function(df, filter_column, filter_value){
filtered_df <- filter(df, {{ filter_column }} == filter_value)
return(filtered_df)
}
How do you use this function to get the rows of df where col_name is equal to "A"?
function_name(df, col_name, "A")
Column names are passed without quotes, just like in other tidyverse functions.