A function accepts input arguments and produces output by executing valid R commands present in the function.
function()Syntax :
f = function(arguments){
statements
return(argument)
}
Example :
We have already created a R-script file that consists of various custom function like volcylinder() , volsacylinder_mimo() etc. Now, in order to apply them, we need to call and load the function file and then use it.
Let’s calculate the volume of a cylinder that has a diameter of \(15 units\) and length of \(30 units\), by using the custom function volcylinder()
source("../User_Defined_Function_Files/Custom_Functions.R") #Invoking and loading the custom functions
v = volcylinder(15,30)
v
## [1] 5301.438
We can pass arguments to the function in the following ways :
v = volcylinder(15,30) #Here, dia = 15 & len = 30
v
## [1] 5301.438
v = volcylinder(len = 30, dia = 15)
v
## [1] 5301.438
v = volcylinder() #Here, dia = 5 & len = 10 has been taken as arguments
v
## [1] 1963.495
Functions are lazily evaluated, which means that if some arguments are missing, then, the function is still executed as long as the execution doesn’t involve these arguments
Example :
volume_case1 = function(dia, len, rad){
vol = pi * dia/2 * len/4
return(vol)
}
vol = volume_case1(len=22, dia=12)
vol
## [1] 103.6726
In the above case, even if we didn’t gave any value for the rad argument, the function executed correctly as the rad argument is not used anywhere.
But, in the below example; we will get an error because, the rad argument has been used in the function but, missing in our evaluation statement.
volume_case2 = function(dia, len, rad){
vol = pi * dia/2 * len/4
print(rad)
return(vol)
}
vol = volume_case2(len=22, dia=12)
vol
Functions in R can take multiple input objects but, returns only one object as output.However, this is not a limitation because, list objects can also be returned by a function.
Example :
We have created a custom function called volsacylinder_mimo() to calculate both volume and surface area of a cylinder by taking diameter and length of the cylinder as input.
source("../User_Defined_Function_Files/Custom_Functions.R")
result = volsacylinder_mimo(dia = 12, len = 15)
result["Volume"] #Getting only volume
## $Volume
## [1] 1696.46
result["Surface Area"] #Getting only surface area
## $`Surface Area`
## [1] 565.4867
Inline functions comes very handy when we have to find out the output of a complex polynomial equation for various input values
Example :
func = function(x){x^2+4*log(x)+5*x+2}
func(5)
## [1] 58.43775
func(13)
## [1] 246.2598
func(23)
## [1] 658.542
There are few looping functions available in R that are pretty useful when working interactively on a command line.
Those functions are as follows :
apply() : Apply a function over the margins of an array or, matrix.lapply() : Apply a function over a list or, vector.tapply() : Apply a function over a ragged array.mapply() : Multivariate version of lapply().xxply() : Function from plyr package.apply() FunctionApplies a given function over the margins of given array.
Syntax :
apply(array, margins, function, ...)
Here, margins refer to the dimension of the array along which the function need to be applied.
margin = 1margin = 2Example :
K = matrix(1:9,3,3)
K
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
apply(K,1,sum) #applying the sum function across the rows
## [1] 12 15 18
apply(K,2,sum) #applying the sum function across the columns
## [1] 6 15 24
lapply() FunctionSyntax :
lapply(list, function, ...)
Example :
Let’s take 2 matrices as the elements of a list and find their determinant in the output using in-built det function
R = matrix(1:9,3,3)
S = matrix(10:18,3,3)
MyList = list(R,S)
determinant_list = lapply(MyList, det)
determinant_list
## [[1]]
## [1] 0
##
## [[2]]
## [1] 5.329071e-15
mapply() Functionlapply().Syntax :
mapply(function, list1,list2,list3 ...)
Example :
Let’s apply the custom function volcylinder() over a set of diameters and lengths
source("../User_Defined_Function_Files/Custom_Functions.R")
dia = list(1,2,3,4,5,6)
len = list(7,8,9,10,11,12)
vol = mapply(volcylinder,dia,len)
vol
## [1] 5.497787 25.132741 63.617251 125.663706 215.984495 339.292007
tapply() FunctionUsed to apply a function over subset of vectors given by a combination of factors.
Syntax :
tapply(vector, factors, function ...)
Example :
Let’s apply the sum function to add the VALUES by ID
ID = c(1,1,1,1,2,2,2,3,3)
VALUES = c(1:9)
tapply(VALUES, ID, sum)
## 1 2 3
## 10 18 17