Today we will…
Follow along
Remember to download, save, and open up the starter notes for this week!
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.
return()
Your function will give back what would normally print out…
return()
If you need to return more than one object from a function, wrap those objects in a list.
Start Practicing!
Write a function to divide each element in a vector by the smallest element and round the results to the nearest whole number and return the resulting vector.
Write a function that, for each element in a vector, returns TRUE
if the number is NOT divisible by 9 or 12, and returns FALSE
otherwise
Think through…
If we supply a default value when defining the function, the argument is optional when calling the function.
something
defaults to 2.When a function requires an input of a specific data type, check that the supplied argument is valid.
add_something <- function(x, something){
if(!is.numeric(x)){
stop("Please provide a numeric input for the x argument.")
}
return(x + something)
}
add_something(x = "statistics", something = 5)
Error in add_something(x = "statistics", something = 5): Please provide a numeric input for the x argument.
add_something <- function(x, something){
if(!is.numeric(x) | !is.numeric(something)){
stop("Please provide numeric inputs for both arguments.")
}
return(x + something)
}
add_something(x = 2, something = "R")
Error in add_something(x = 2, something = "R"): Please provide numeric inputs for both arguments.
Practicing with optional v. required arguments
Write a function called every_other()
. This function should take in a vector and return every other value in the vector. Include an optional argument called start
which lets you choose where to start skipping; that is, if start = 1
, the function returns the 1st, 3rd, 5th, etc. values and if start = 2
, the function returns the 2nd, 4th, 6th, etc. values.
Add some input validation if you have time!
first argument: vec
should be a vector (length more than 1?)
second argument: start
only takes values of 1
or 2
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!
Finish PA7
Write a function called shorten()
. This function should take in a vector, and only return values from the original vector for which the cumulative sum for that element is greater than a provided number.
Think through for your 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.
Write a function called find_car_make()
that takes in the name of a car and returns the “make” of the car (the company that created it).
find_car_make("Toyota Camry")
should return “Toyota”.find_car_make("Ford Anglica")
should return “Ford”.