Today we will…
Follow along
Remember to download, save, and open up the handout for today!
Functions allow you to automate common tasks!
Writing functions has 3 big advantages over copy-paste:
Let’s define the function.
add_two <-The name of the function is chosen by the author.
The argument(s) of the function are chosen by the author.
{ }The body of the function is where the action happens.
Your function will give back what would normally print out from the last line in the body…
return()…you can also explicitly use the command return().
Style decision
The tidyverse style guide currently suggests using implicit returns since it is more concise and more “idiomatic” to R…
But using return() explicity is clearer to readers and new learners.
I leave this up to you!
Explicit returns are necessary for early returns
If you need to return more than one object from a function, wrap those objects in a list.
What if we wanted to write a more general function, named add_something(). The function would take two inputs:
x the vector to add tosomething the value to add to xHow would your function change?
something defaults to 2.When a function requires an input of a specific data type, check that the supplied argument is valid.
The location (environment) in which we can find and access a variable is called its scope.
We cannot access variables created inside a function outside of the function.
Name masking occurs when an object in the function environment has the same name as an object in the global environment.
Functions look for objects FIRST in the function environment and SECOND in the global environment.
It is not good practice to rely on global environment objects inside a function!
(Allison Horst)
You will make mistakes (create bugs) when coding.
print() debugging
print() statements throughout your code to make sure the values are what you expect.When you have a concept that you want to turn into a function…
Write a simple example of the code without the function framework.
Generalize the example by assigning variables.
Write the code into a function.
Call the function on the desired arguments
This structure allows you to address issues as you go.
[ ]TRUE, FALSE)1, 2, 3)above_average()Goal: Keep only the elements of
xgreater than the mean.
Step 1: Find locations where values of x are larger than the mean
Step 2: Use this output to extract the desired values from x
every_third()Goal: Return every third element from a vector.
Write down the steps you would need to create a function named every_third() that takes in a vector and returns every third element from that vector (i.e., indices 1, 4, 7, 10, etc.).
Think about:
Represent the indices (positions) of each element of
x.
Identify which positions are “every third.”
| index | Remainder (index %% 3) |
Keep? |
|---|---|---|
| 1 | 1 | ✅ |
| 2 | 2 | ❌ |
| 3 | 0 | ❌ |
| 4 | 1 | ✅ |
| 5 | 2 | ❌ |
| 6 | 0 | ❌ |
| 7 | 1 | ✅ |
Identify which positions are “every third.”
xGrab the elements of
xwe want to keep.
You will write several small functions, then use them to unscramble a message. Many of the functions have been started for you, but none of them are complete as is.

During your collaboration, your group will alternate between three roles:
Starting Roles Today
The person who has the most siblings starts as the coder, second as the project manager.