Creating Functions in R

Author

Greg Martin

1 Introduction

Functions are one of the most powerful features in R programming. They allow you to create reusable blocks of code that can be called multiple times with different inputs. This tutorial will walk you through creating functions in R, starting with the simplest examples and building up to more complex scenarios.

function structure

function_name <- function(argument1, argument2, …) {

Function body - the code that does the work using the values provided by the arguments

}

2 The Key Components:

  • Function Name: The name you give your function (like add_numbers or calculate_average)

  • Assignment Operator (<-): This assigns your function to the name so you can use it later

  • function() Keyword: This tells R you’re creating a function

  • Arguments/Parameters: The inputs your function accepts, listed inside the parentheses

  • Curly Braces ({}): These wrap around the function body and define what code belongs to the function

  • Function Body: The actual code that performs the task

  • Return Value: What the function gives back when it’s finished (can be explicit with return() or implicit)

3 How Functions Work:

  • You define the function once using the structure above

  • You call the function by typing its name followed by parentheses containing the inputs

  • R executes the code inside the function body

  • The function returns a result that you can use or store

Think of functions like recipes: you write the recipe once (define the function), but you can cook the dish many times (call the function) with different ingredients (arguments) to get similar but customized results.

Let’s start with the simplest possible function - one that takes two numbers and adds them together.

add_numbers <- function(x, y) {
  x + y
}

add_numbers(5, 3)
1
We create a function called add_numbers that takes two arguments: x and y
2
The function body simply adds the two arguments together
3
The closing brace marks the end of the function definition
4
We call the function with the values 5 and 3, which returns 8
[1] 8

Functions can have default values for their parameters, making them more flexible to use.

hello <- function(x, y = "Hey there") {
  paste(y, x, sep = " ")
}

hello("Greg")
1
The parameter y has a default value of “Hey there” - if no value is provided for y, this default will be used
2
We use paste() to combine the two strings with a space separator
3
When we call the function with just “Greg”, it uses the default value for y
[1] "Hey there Greg"

The ... (dots) parameter allows functions to accept any number of arguments, making them very flexible.

string <- function(..., x = " ") {
  paste(..., sep = x)
}

string("a", "b", "c")
string("a", "b", "c", x = "-")
1
The ... allows any number of arguments to be passed, and x has a default separator of a space
2
All arguments passed via ... are combined using the separator specified by x
3
Using the default separator (space) to join three letters
4
Overriding the default separator to use a dash instead
[1] "a b c"
[1] "a-b-c"

Functions become really powerful when working with real datasets. Here we process the built-in chickwts dataset.

find_max_value <- function(x){
  max_value <- max(x, na.rm = TRUE)
  paste("The highest value is", max_value, sep = " ")
}

find_max_value(chickwts$weight)
1
Function takes a vector x as input
2
max() finds the highest value, na.rm = TRUE ignores any missing values
3
We create a descriptive message using paste() to combine text with the result
4
We pass the weight column from the chickwts dataset to find the heaviest chick
[1] "The highest value is 423"

Functions can include conditional logic using ifelse() to make decisions based on the input data.

check <- function(value) {
ifelse(value > 200, "High", "Low")
}

check(chickwts$weight)
1
Function takes a value parameter (which can be a single value or vector)
2
ifelse() checks if each value is greater than 200, returning “High” if true, “Low” if false
3
We apply this function to all weights in the chickwts dataset, getting a vector of “High”/“Low” classifications
 [1] "Low"  "Low"  "Low"  "High" "High" "Low"  "Low"  "Low"  "Low"  "Low" 
[11] "High" "High" "Low"  "Low"  "High" "High" "Low"  "Low"  "High" "High"
[21] "High" "High" "High" "High" "High" "High" "High" "High" "Low"  "High"
[31] "High" "High" "Low"  "Low"  "Low"  "High" "High" "High" "High" "High"
[41] "High" "High" "High" "High" "High" "High" "High" "High" "High" "High"
[51] "High" "High" "High" "Low"  "High" "High" "High" "High" "High" "High"
[61] "High" "High" "High" "High" "High" "High" "High" "High" "High" "High"
[71] "High"

4 Summary

You’ve now learned the key concepts of creating functions in R:

  • Basic syntax: function_name <- function(arguments) { code }
  • Default parameters: Making arguments optional with default values
  • Flexible arguments: Using ... to accept varying numbers of inputs
  • Data processing: Working with real datasets and vectors
  • Conditional logic: Using ifelse() for decision-making within functions

Functions make your code more organized, reusable, and easier to maintain. Practice creating your own functions with these patterns!