Function
Overview
Teaching: 10 min
Exercises: 0 minQuestions
How to write function in R?
How can I use the function from packages
Objectives
Define function
Learn to create
nestedfunctionReturn value(s) from function
Check argument conditions with
stopifnot()function
A function is a set of scripts organized together to carry out a specific task. Writing efficient functions is an important skill that can significantly improve the productivity of data scientists and data science solutions. In this guide, you will learn the basics of writing a function and the types of functions, which will enable you perform analytical tasks more efficiently.
Using custom functions
There are several in-built functions in R that can be used to perform analytical tasks, for example: mean, min, max, quantile,summary.
Detail on mean() function below:
mean(arg)
Using function mean with missing value
v <- c(2,NA,4,NaN,6)
mean(v,na.rm=TRUE)
Using User-Define a function with 1 arg(ument)
Syntax:
f <- function(arg){
do function with argument
}
Example:
squareroot <- function(a){
a^0.5
}
squareroot(49)
Using User-Define a function with 2 or more arg(uments)
Syntax:
f <- function(arg1,arg2){
do function with arg1 & arg2
}
Example:
Addtwo <- function(a,b){
a+b
}
Addtwo(1,2)
Return specific value from function
Syntax:
f <- function(args){
f1 <- do function with args
return(f1)
}
For example:
# Function to convert oF to oC
F2C <- function(temp){
c <- ((temp - 32) * (5 / 9))
return(c)
}
F2C(100)
Return list of (more) values from function
Syntax:
f <- function(args){
do function with args
out1 <- do1
out2 <- do2
output <- list(out1=out1,out2=out2)
}
Example:
sqsum <- function(a){
sq <- a^0.5
sumsq <- a+sq
output <- list(sq=sq, sumsq=sumsq)
}
sqsum(49)
Nested function
In complex data science use cases, we may have to work on nested functions, which contain functions within a function.
For example: Given dataset mtcars. Find the mean of fuel consumption mpg for cars that having 4 cylinders cyl
data(mtcars)
names(mtcars)
# Step 1: find the cars that having 4 cylinders:
ind1 <- mtcars$cyl==4
# Step 2: find the fuel consumption of all the cars having 4 cylinders:
ind2 <- mtcars$mpg[ind1]
# Step 3: compute the mean
mean(ind2)
All the 3 steps can be nested into one command line for experience user:
mean(mtcars$mpg[mtcars$cyl==4])
Defensive programming with stopifnot() function
Defensive programming encourages us to frequently check conditions and throw an error if something is wrong. For example:
F2C <- function(temp){
stopifnot(is.numeric(temp)==TRUE)
c <- ((temp - 32) * (5 / 9))
return(c)
}
F2C(100a)
F2C(100)
Key Points