Types of Functions in R Programming

If you’re a Data Science aspirant looking forward to a career in the field, you need to make sure that you have enormous experience in R programming. Being one of the most important part of Data Science, you need to start coding sooner. 

Throughout your learning journey or your professional career in Data Science, you’ll have to use programming language and you’ll come across different functions. Functions are central part of any programming language including Python, Julia and R. 

The following blog will take you on a journey to understand different type of functions in R and how to write them. It will help you learn about functions in R programming and expand your ability to learn more on the subject. 

What is a Function in R?

In case of programming, functions are organised instructions which carry out a specific task. The purpose of functions is creating self-contained programs that can be called only when needed.

Functions can be used for different reasons and can take various forms. Generally, majority of the functions will take input of data, process it and provide you with a result. Depending on the origin of function, there are three main types of functions in R: 

  • Built-in functions 
  • Functions available in R packages 
  • User-defined functions 

Type of Functions in R:

In R, there are several types of functions based on their usage and behavior. Some of the common types of functions in R are as follows:

  • Built-in Functions: These are functions that come pre-defined in the R language and are available for immediate use. Examples include mean(), sum(), print(), length(), sqrt(), etc.
  • User-defined Functions: These are functions created by users to perform specific tasks. Users can define their own functions using the function() keyword. For example:

User-defined Functions

  • Anonymous Functions (or Lambdas): These are functions without a formal name and are defined using the function() keyword without assigning them to a variable. They are often used in combination with other functions like apply() and lapply().

Anonymous Functions

  • Recursive Functions: These are functions that call themselves within their definition. Recursive functions are useful for solving problems that can be broken down into smaller sub-problems.

Recursive Functions

  • Higher-order Functions: In R, functions are first-class objects, which means they can be assigned to variables and passed as arguments to other functions. Higher-order functions are functions that take one or more functions as arguments or return functions as results.

Higher-order Functions

  • S3 Methods: S3 is a basic object-oriented programming system in R. Functions can be defined to handle specific classes of objects using S3 methods. These methods are identified by the class of the first argument.

S3 Methods

These are some of the main types of functions you’ll encounter in R. Understanding them will help you better organize and modularize your code for more efficient and maintainable programming.

How to Write Functions in R? 

Writing functions in R is a fundamental skill that allows you to encapsulate reusable code and perform specific tasks. To create a function in R, you need to follow these steps:

  1. Use the function() keyword: To define a function, start by using the function() keyword, followed by parentheses containing the arguments (if any) that the function will accept. The general syntax is as follows:
  2. Implement the function body: Inside the function, write the code that performs the desired operations based on the input arguments. You can use any R expressions, control structures (e.g., if, else, for, while), and other functions within the function body.
  3. Return a value (optional): If your function needs to return a result, use the return() statement to specify the value that should be returned. If there is no explicit return() statement, R will automatically return the value of the last expression in the function.
  4. Assign the function to a variable (optional): You can assign the function to a variable name so that you can call it later in your code. If you don’t assign it, the function will still be available for use in the current R session.

Here’s an example of a simple function that calculates the area of a circle:

In this example, the calculate_circle_area() function takes a single argument radius, and it returns the area of the circle calculated using the formula pi * radius^2.

Keep in mind that functions in R can have multiple arguments, can be recursive, and can return different types of data, including vectors, matrices, data frames, and more.

Once you have defined your function, you can call it with different arguments to perform the desired calculations or tasks. Functions greatly enhance the reusability and organization of your code, making it easier to manage and maintain as your projects grow in complexity.


Thus, from the above blog you have come to know about the functions in R programming. If you’re willing to opt for Data Science as your career choice, you need to have a thorough knowledge on types of functions in R. 

The above mentioned R functions list are the typical and widely used functions in R programming. You need to develop your skills in R programming in order to learn the functions and apply them in case of data manipulation and graphical visualisation.