Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
2. Creating a function
Creating functions
If statements
Summary

Instruction

It's easy to create a function in R. Here is a pseudocode template you can use:

function_name <- function(arguments){
  instructions
  return (some value)
}

Before the assignment operator, <-, we define the function name. The function has arguments – inputs it will use in its calculations. These are defined as comma-separated names within parentheses.

Curly braces, {}, denote the start and end of the function body, where the real work is done. The function may choose to return a value (more on that later).

Exercise

Let's start with an example. Suppose we want to see each person's first and last name together in a single string. We'll create a function that takes two inputs, concatenates those inputs into one string, and capitalizes all characters in the result.

Run the code, and see what happens.