User defined functions in R

We can create user defined function by using function() .

We create a function object as:

function_name<- function(argument) {

   #Function Body


The different parts of a function are -

Function Name - This is the actual name of the function . It is stored as an object in R.

Arguments - It is use as a placeholder. When a function is invoked , you pass a value to the argument. Arguments can be optional .

Function Body - The function body contains a collection of statements that defines what the function does.

Return Value - The return value of a function is the last expression in the function body to be evaluated.

We create a function name "f" which is used to calculate square of a number.

f <- function(x,y){



f stores structure of function .

We can called function by passing parameters in to it.

We want to find square of 2. We pass the value as argument to the function.


Similarly , we find square of number -3 .



We create a new function "f" which print two numbers .

add <- function(x,y){




We call function add with two parameters .


We create a function to select observation on base of condition.

Here , we select only those observation where vector x values greater than n.

above <- function(x, n){

       sub <- x > n



a <- c(2,3,4,6,11,22,4,56,6)

We call function on vector "a" . We want to show values in "a" greater than 4.


We are providing n=5 value in function argument .

x <- 1:10

above <- function(x, n= 5){   

       sub <- x > n



We invoked above function as :


It shows values greater than 5.


We create function to find means of each column of y . We use for loop to find means of all columns of  y.

colmean <- function(y){

       nc <- ncol(y)

       means <- as.numeric()

       for(i in 1: nc){

               means[i] <- mean(y[,i])




We want to find means of each column of airquality dataset.



We create a character vector "names1" .

names1 <- c("Dave", "John", "Ann", "Roger", "Bill", "Kathy")

Here , we are using break statement to stop loop when name equal to "Roger" . We use for loop to go through all elements of x . We use if to check if name equals to "Roger" . If the condition is TRUE then the loop is break .

f.names <- function(x) {

       for(name in x){






We call function by passing argument as names1 . So  , it shows three names.



We create a simple function to create two new variables z1 and z2 . We use list() to store z1 and z2 as result1 and result2 lists.

f4 <- function(x=3, y=2) {

       z1 <- x + y

       z2 <- x + 2*y

       list(result1=z1, result2=z2)



We called function in different forms as :


Here , we called function by default parameters . So, here x=3 and y=2 .

f4(1,  )$result1

We called function with x=1 and y=2 as default parameter . We only want to see result1 value.


We called function with x=1 and y=2 as default parameter . We only want to see result1 value.

f4(, 1)$result1

We called function with x=3 as default parameter and y=1 . We only want to see result1 value.



We called function with x=3 as default parameter and y=1 . We only want to see result1 value.

f4(y = 1, x = 2)$result2

We called function with x=2 and y=1 . We only want to see result2 value.


Visit our New Website by clicking the Logo Above or and you can earn as a Trainer - if interested in Delivering training or Contribute to the Learner Community by sending your own Blogs that will be posted here -

Subscribe to our mailing list

* indicates required

Looking for Corporate Training ? Reach out to us at

Reach out to us if you are looking for Corporate Training to Build The Next Generation Analytical Workforce with an in-depth understanding of  Exploratory Data Analysis , Data Visualisation, Data Analytics , AI First , Machine Learning & Deep Learning Training & Consulting helping them to take Data Informed Decision at each stage of the business. 

We understand that At the present times , the Entire Industry is in a Tranformation stage with the Softwares  being rebuilt with Artificial Intelligence Capabilities .

We need SMART WORKFORCE for the SMART SOFTWARES to reap the maximum return . 


Whatsapp at +91-9953805788 or email at - if you would like to know more . 


  • Corporate Tableau Training in Gurgaon
  • Corporate Data Analytics Training in Gurgaon
  • Corporate Microsoft Power BI Training in Gurgaon
  • Corporate Microstrategy Training in Gurgaon
  • Corporate Google Data Studio Training in Gurgaon
  • Corporate Python Training in Gurgaon
  • Corporate Advance Analytics in R Programming Training in Gurgaon
  • Corporate Machine Learning Training in Gurgaon
  • Corporate Deep Learning Training in Gurgaon
  • Corporate Data Visualization Training in Gurgaon



Instrovate Technologies

Noida, Gurgaon 

Hit Your Refresh Button To Rise Higher
Print Print | Sitemap
All rights reserved @ Instrovate Technologies

Terms Of Services    Privacy Policy    Disclaimer     Refund Policy