r functions

R Functions

Posted on

Functions in R

A function consists of a series of directives intended to accomplish a particular objective. Within a programming language, a function modifies the given parameters into a preferred format. For instance, let’s take an addition function: it computes the sum of the provided arguments, where the computation represents the transformation, and the resulting sum represents the desired format of the arguments. The R programming language offers functions to organize a set of directives and accomplish a task. In R, functions are categorized into two types: Built-in R functions and User-defined R functions.

Built-in R functions

Every programming language is developed to fulfill specific needs and evolves according to its own goals. Given that the R programming language is primarily designed for data analytics, it offers a wide range of commonly used analytical functions as built-in features. This ensures accessibility for developers and eliminates the need to repeatedly write basic functions.

Some examples of these built-in R functions:

  • str() – shows the structure of an R object
  • print() – displays an R object on the console
  • min()max()mean()median() – return the minimum / maximum / mean / median value of a numeric vector, correspondingly
  • sum() – returns the sum of a numeric vector
  • range() – returns the minimum and maximum values of a numeric vector
  • sort() – sorts a vector in ascending or descending (decreasing=TRUE) order
  • abs() – returns the absolute value of a number
  • length() – returns the number of items in an R object (a vector, a list, etc.)
  • ncol() – returns the number of columns of a matrix or a dataframe
  • nchar() – returns the number of characters in a character object
  • exists() – returns TRUE or FALSE depending on a variable is defined or not, in the R environment

User-defined R functions

Certain situations may require you to create functions, whether it’s to implement a new algorithm or define specific business logic. In these instances, the R programming language enables you to write custom functions.

Syntax:

function_name <- function(argument1, argument2, ...) {
  function body or code to be executed
}

function_name – Mandatory: the name of the function to be used for referencing it throughout the rest of the code.
arguments (argument1, argument2, …) – Optional: the values intended for use within the function block.
function_body – Optional: a group of statements that collectively execute a task. The function’s body may include a return_value, representing the ultimate outcome derived from transforming the arguments.

There are three scenarios in this tutorial related to user-defined functions:

  • R function with no arguments
  • R function with arguments
  • R function that returns a value

After defining a function, you can call it from another section of the R script file.

Example – R function with no arguments

#Define function:
sayHi<- function(){
    print("Hi!")
}

#Call the function:
sayHi()

#output:
[1] "Hi!"

Example – R function with arguments

#Define function:
avg <- function(m, n) {
  (m + n) / 2
}

#Call the function by passing in the two numbers into the arguments m and n:
avg(6,8)

#output:
[1] 7

Example – R function that returns a value

#Define function:
sum_abc <-function(a,b,c){
  return (a+b+c)
}

#Call the function by passing in the three numbers into the arguments a, b and c
d <- sum_abc(2,7,9)
print(d)

#output:
[1] 18

Conclusion

We have passed R tutorial about functions and how to use it.