Overview
Teaching: 30 min
Exercises: 10 minQuestions
What is a function?
How do functions help us with reproducibility?
How do I write a function in
R
?Objectives
Learn about functions
Learn how to write a function in
R
Overview
What is a function?
input --> function does something --> output
One of the main benefits of writing functions is to avoid repetition of your code. When you write code you should aim for laziness. If you are about to copy a piece of code 5 times and just change a variable in each instance, you are better off converting it into a function. In general, you should never repeat yourself when you write code, it’s called the DRY (Don’t Repeat Yourself) principle.
Another advantage is that your code is self contained and any variable that you create inside a function will not be exported into your global environment.
(70 - 32) * (5 / 9)
## [1] 21.11111
(65 - 32) * (5 / 9)
## [1] 18.33333
(85 - 32) * (5 / 9)
## [1] 29.44444
function
:fahr_to_celsius <- function(temp) {
(temp - 32) * (5 / 9)
}
### Don’t forget to re-evaluate your function, after modifying it.
convert_fahr <- function(temp, to) {
res <- (temp - 32) * (5 / 9)
if (to == "kelvin") {
res <- res + 273.15
}
res
}
convert_fahr <- function(temp, to = c("celsius", "kelvin")) {
to <- tolower(to)
to <- match.arg(to)
res <- (temp - 32) * (5 / 9)
if (to == "kelvin") {
res <- res + 273.15
}
res
}
fahr_to_celsius <- function(temp) {
(temp - 32) * (5 / 9)
}
celsius_to_kelvin <- function(temp) {
temp + 273.15
}
convert_fahr <- function(temp, to = c("celsius", "kelvin")) {
to <- tolower(to)
to <- match.arg(to)
res <- fahr_to_celsius(temp)
if (to == "kelvin") {
res <- celsius_to_kelvin(res)
}
res
}
Your intentions are clear, everything is self contained, you can easily debug, test and document each of the steps involved.
Converter challenge
Write a function that converts pounds in kilograms (divides pounds by 2.2).
Stretch goal: and in grams
Solution
This is the body of the solution.
lb_to_kg <- function(pounds){ kg <- pounds * 2.2 return(kg) }
In grams:
lb_to_kg <- function(pounds){ gm <- pounds * 2.2 * 1000 return(gm) }
Key Points
Functions complete tasks in a reproducible manner
Functions improve reproducibility