R Functions

Spread the love

This article aims to provide a detailed understanding of functions in R, both built-in and user-defined.

What are Functions?

In programming, a function is a named section of a program that performs a specific task. Functions in R are objects that contain a sequence of statements. They take input in the form of arguments, perform computations on these inputs, and return a result. Functions are fundamental to R programming and allow for code reuse and abstraction.

Using Built-in Functions in R

R comes with a plethora of built-in functions that perform a wide array of tasks. For instance, mathematical operations such as square roots (sqrt()), exponentiation (exp()), and trigonometric functions (sin(), cos(), tan()), statistical operations such as mean (mean()), median (median()), variance (var()), and standard deviation (sd()), as well as data manipulation operations like sorting (sort()) and ordering (order()).

Here’s a simple example of using a built-in function, mean():

numbers <- c(1, 2, 3, 4, 5)
average <- mean(numbers)

In this code, mean() calculates the average of the numbers in the vector numbers.

Defining Your Own Functions in R

While R’s built-in functions are extensive, there may be situations where you need to define your own function to perform a specific task not covered by the existing functions. User-defined functions help to make code more readable and reusable.

The structure of a user-defined function in R is as follows:

function_name <- function(arg1, arg2, ..., argn) {
  # Code that uses arguments to perform an operation
  # ...

Here’s an example of a user-defined function that calculates the sum of squares of two numbers:

sum_of_squares <- function(a, b) {
  result <- a^2 + b^2

print(sum_of_squares(3, 4))

In this example, sum_of_squares is the function, a and b are arguments, and result is the value returned by the function.

Function Arguments

Functions in R can take multiple forms of arguments:

  • Required arguments: These are the arguments that must be passed to the function for it to work correctly. In the sum_of_squares function above, a and b are required arguments.
  • Optional arguments with default values: You can specify default values for some or all arguments. These arguments become optional when calling the function.
sum_of_squares_or_cubes <- function(a, b, cube=FALSE) {
  if (cube) {
    result <- a^3 + b^3
  } else {
    result <- a^2 + b^2

print(sum_of_squares_or_cubes(3, 4, TRUE))

In this example, cube is an optional argument with a default value of FALSE. If cube is not provided when calling the function, it takes the default value.

  • Variable-length argument lists: If the number of arguments is unknown or can vary, ... can be used to denote variable length arguments.
product <- function(...) {
  numbers <- c(...)

print(product(1, 2, 3, 4, 5))

In this example, ... allows for any number of arguments to be passed to the function.

Scoping Rules

R uses lexical scoping or static scoping. Variables in R are either local or global. Variables defined inside a function are local to that function and are not accessible outside the function. Conversely, variables defined outside any function are globally scoped and can be accessed anywhere in the script. However, global variables can be used inside functions if they are not defined locally.

global_var <- "Hello"

my_function <- function() {
  local_var <- "World"
  print(paste(global_var, local_var))


In this example, global_var is a global variable, and local_var is a local variable. global_var can be accessed inside my_function, but local_var would not be accessible outside my_function.


Functions in R, whether built-in or user-defined, are crucial for carrying out computations and operations in a structured and reusable manner. They allow for code to be organized, reusable, and scalable. A deep understanding of how to use and define functions in R is pivotal to becoming proficient in the language.

Posted in RTagged

Leave a Reply