Answer
# create function #
<- function(x) {
double_it return(2 * x)
}
# try it out #
# explicit notation
double_it(x = 24)
# non explicit notation
double_it(24)
These exercises are adapted from Allison Horst’s EDS 221: Scientific Programming Essentials Course for the Bren School’s Master of Environmental Data Science program.
We’re going to start by creating some simple functions. Recall that the anatomy of a function is the same for all functions and each one contains:
{}
Create a function called double_it()
that doubles any value input value. Then try out the function, are the values returned what you expect?
# create function #
<- function(x) {
double_it return(2 * x)
}
# try it out #
# explicit notation
double_it(x = 24)
# non explicit notation
double_it(24)
Write a function called exclaim_age()
that returns the statement “I am ___ years old!”, where the blank is entered by the user as argument age.
Then try out the function, are the values returned what you expect?
# write function #
<- function(age) {
exclaim_age return(paste("I am", age, "years old!"))
}
# try it out #
# explicit notation
exclaim_age(age = 12)
# non explicit notation
exclaim_age(12)
Consider the function called find_max()
:
# "find_max() function" #
<- function(value_1, value_2) {
find_max
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
} }
# example using `find_max()` in an arithmetic expression #
# we expect the answer to be 20
5 * find_max(4, 2)
Run find_max(4, 2, 5)
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, 2, 5)
, the following error message appears:
Error in find_max(4, 2, 5) : unused argument (5)
This is an error message that is automatically created by R since our function only requires two parameters. This is a sufficient error message.
Run find_max(4, 4)
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, 4)
, no message appears and the function is sent to the Console, but no value is either returned or printed.
To account for this scenario, add an if()
statement to the beginning of the function, and then use either warning()
or stop()
.
# `find_max()` function with error message
<- function(value_1, value_2) {
find_max
if (value_1 == value_2) {
stop("Values must be different from each other.")
}
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
}
}
# try it out #
# does the message appear as you expected?
find_max(4, 4)
Run find_max(4, "cow")
in the Console.
What happens? What kind of message appears? Is it sufficient? If not, consider adding a warning or error message using warning()
or stop()
. Remember, use ?function
to access the Help page. Add additional logic to the function, as needed.
When you run find_max(4, "cow")
, the function runs as is and returns the value “cow”. This is not expected because these two values aren’t necessarily comparable since they’re different data types.
However, find_max()
doesn’t know any better since this scenario hasn’t been defined in the body of the function yet.
To account for this scenario, add additional logic that checks the class of each argument before the function continues to execute.
The logical operator for OR is |
. The not-equal-to operator is !=
.
# `find_max()` function with error messages and checks
<- function(value_1, value_2) {
find_max
# `|` is the logical OR operator
# `!=` is the not-equal-to operator
if (is.numeric(value_1) != TRUE | is.numeric(value_2) != TRUE) {
# alt expression: is.numeric(value_1) == FALSE | is.numeric(value_2) == FALSE
stop("Value must be a numeric type.")
}
if (value_1 == value_2) {
stop("Values must be different from each other.")
}
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
}
}
# try it out #
# does the message appear as you expected?
find_max(4, "cow")
find_max("cow", 4)
Run find_max(4, 4)
in the Console. Previously we coded our function to report an error. But perhaps the user would prefer to have the function return the shared value, as an option. Add an argument with a reasonable default value to allow the user to control this behavior. Add additional logic to the function, as needed.
Before, if value_1 == value_2
, we used stop()
to create an error. But with an additional argument, we can adjust how the function responds by testing the value of that argument.
Often for arguments that turn on or turn off a behavior, a TRUE/FALSE value makes sense so you could easily include the argument in a logical test.
# `find_max()` function with error messages and checks
<- function(value_1, value_2, equal_ok = FALSE) {
find_max
# `|` is the logical OR operator
# `!=` is the not-equal-to operator
if (is.numeric(value_1) != TRUE | is.numeric(value_2) != TRUE) {
# alt expression: is.numeric(value_1) == FALSE | is.numeric(value_2) == FALSE
stop("Value must be a numeric type.")
}
if (value_1 == value_2) {
### the values are equal; is value of the equal_ok argument TRUE?
if(equal_ok) return(value_1)
### if equal_ok is not TRUE, then report an error
stop("Values must be different from each other.")
}
if (value_1 > value_2) {
return(value_1)
}else if (value_2 > value_1) {
return(value_2)
}
}
# try it out #
# does the message appear as you expected?
find_max(4, 4)
find_max(4, 4, equal_ok = TRUE)