03:00
Today we will…
[] refresherseq_along()if() and else if() refresherFunctions allow you to automate common tasks!
Writing functions has three big advantages over copy-paste:


Write a function named add_two() that will add 2 to whatever number is input.
03:00
Compare Your Function with Your Neighbor
In what ways are your functions the same? In what ways do they differ?
The name of the function is chosen by the author.
The argument(s) of the function are chosen by the author.
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?
02:00
If we do not supply a default value when defining the function, the argument is required when calling the function.
If we supply a default value when defining the function, the argument is optional when calling the function.
A lot of the functions we’ve been working with so far actually have a lot of optional arguments:
mean(x,
trim = 0,
na.rm = FALSE, ...)
max(..., na.rm = FALSE)
min(..., na.rm = FALSE)
geom_point(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
{ }The body of the function is where the action happens.
Your function will give back what would normally print out…
…but some of us might prefer an explicit return().
return()If you are coming to R from a background in Python, C, or Java then an explicit return may feel more natural to you.
return()s✅ Pros of Using return()
⚠️ Cons of Using return()
✅ Pros of Implicit Return (no return())
⚠️ Cons of Implicit Return
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
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:
03:00
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.
This activity will require knowledge of:
[] and logical values to extract elements of a vectorif () & else if() statements
None of us have all these abilities. Each of us has some of these abilities.

During the Practice Activity, you are not permitted to use Google or ChatGPT for help.
You are permitted to use:
base R cheatsheet, andSubmit the name of the television show the six numbers are asssociated with.

The partner who has the most siblings starts as the Talker!

The partner who has the most siblings starts as the Talker!

When a function requires an input of a specific data type, check that the supplied argument is valid.
Error in add_something(x = "statistics", something = 5): Please provide a numeric input for the x argument.
How would you modify the previous code to validate both x and something?
Meaning, the function should check if both x and something are numeric.
Error in add_something(x = 2, something = "R"): Please provide numeric inputs for both arguments.
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!