25.2 When and how to write a function

Have you copy and pasted code more than 2x? Consider a function!

Key steps in creating a function:

  1. Pick a name than makes it clear what the function does

  2. Arguments, or input variable(s), go inside function, like so function(arguments).

  3. The code goes inside curly braces { }, after function().

  4. Check your function with a few inputs to make sure it’s working.

name <- function(arguments) {
  code
}