Writing Functions in R

Functions are one of the most important tools in R. They let you package a sequence of operations into a single reusable command, reducing repetition and making your code easier to read and maintain.

The general structure of a function in R is as follows:

function_name <- function(arg1, arg2 = default_value) {
  # body: operations using the arguments
  return(output)
}

A function definition has three main components:

The return() statement specifies what the function should output, but it is optional. If omitted, R automatically returns the value of the last evaluated expression.

A simple example:

square <- function(x) {
  x^2
}

square(4)
[1] 16

This function takes one argument (x), squares it, and returns the result. You can store the output or use it directly in a calculation:

square(3) + 2
[1] 11

In R, functions are objects just like numbers, vectors, or data frames. You can print them, assign them, or inspect their content:

square
function (x) 
{
    x^2
}
<bytecode: 0x62116769b748>

The main reason to write your own functions is to avoid repeating the same code. If you notice yourself copying and pasting the same few lines several times, that’s a good sign that those lines should become a function instead.

Functions help you to make the code more reusable, using one definition that can be called multiple times, to make the code more readable, using meaningful names to describe what the code does, and to encapsulate complexity, allowing you to hide the details of a process behind a simple interface.

For instance, suppose you frequently rescale numeric variables to a 0–1 range. Instead of repeating the logic each time, you can define a function once and call it whenever needed:

rescale <- function(x, na.rm = TRUE) {
  rng <- range(x, na.rm = na.rm)
  (x - rng[1]) / (rng[2] - rng[1])
}
rescale(c(1, 5, 10))
[1] 0.0000000 0.4444444 1.0000000

This function now performs a well-defined transformation in a single, descriptive call.

Practice

Write a function that takes a temperature in Fahrenheit as input and returns the equivalent temperature in Celsius. The formula to convert Fahrenheit to Celsius is: C = (F - 32) * 5/9

fahrenheit_to_celsius <- function(f) {
  (f - 32) * 5 / 9
}
Back to top