Repetitively Apply User Defined Functions in R

Developed with
Start course

This module looks at conditional statements in R, such as for loops and how to repeat functions.   

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • When to use a for loop in R 
  • How to nest a for loop  
  • Built-in functions being vectorized
  • How to apply functions
  • How to use the family of apply functions  

Intended Audience 

Aimed at anyone who wishes to learn the R programming language.


No prior knowledge of R is assumed. You should already be familiar with basic programming concepts such as variables, scope, and functions. Experience of another scripting language such as Python or Perl would be an advantage. An understanding of mathematical concepts would be beneficial.


We welcome all feedback and suggestions - please contact us at to let us know what you think. 


We can use user-defined functions, or UDFs, as part of the fun argument of the apply. First, let me define a matrix to help us utilize the apply function, called toys. And I'd like to know, what is the standard error in the cost of these toys, assuming these are pounds, or. We might want to use a function that we have created, for this standard error. So I might want to use this later on, somewhere else, so I will create a special st.err function, where I calculate the standard error. And then, I can call on the apply function to help me utilize the toys data set, with the MARGIN = 2, because I'm looking at the columns. And I would like to utilize my function that I have defined. And in each column, I can see that we have a standard error, as such. Another simple UDF might be that I'd like to know the average between two vectors. As part of my input to this function VECKY, I could ask for the first and the third vector to be averaged. This is an arbitrary function, just to show you how chaotic user-defined functions can be. And if I wanted to, say, for example, look at my toy matrix, and understand the connection between the first row, Kunal, and the third row, Charles, and if I wanted to know the average of the costs of their toys, I could use, instead of using the standard error, I could now replace that with this user-defined function called AVERAGE1n3. And this would tell me the average of the toys' costs between Kunal and Charles. So, for example, one plus seven, divided by two, is four.


About the Author
Kunal Haria
Data Science Trainer
Learning Paths

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.