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 objectprint()– displays an R object on the consolemin(),max(),mean(),median()– return the minimum / maximum / mean / median value of a numeric vector, correspondinglysum()– returns the sum of a numeric vectorrange()– returns the minimum and maximum values of a numeric vectorsort()– sorts a vector in ascending or descending (decreasing=TRUE) orderabs()– returns the absolute value of a numberlength()– 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 dataframenchar()– returns the number of characters in a character objectexists()– returnsTRUEorFALSEdepending 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.
