Repetitively Apply Anonymous 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 repetitively apply a function to a matrix, anonymously. In order to understand that, let me create a matrix of, say, for example, toys. Here we have the type of toy in the columns and the rows represent each of our different users. If I wanted to understand the average of the first and the last entry in each column, so, I want to understand the average between one and seven for cars, meaning, I'd like to know the average per the two rows for Kunal, in row one and Charles, in row three. I could do this by defining a function and using, say for example, the key words, "average" over one and three, meaning over the first and third row. And I could assume that I am applying something into this function and with that something I've taken the first and the third element and I am taking the average. In this case of my problem, I would want to put- inside of my function would be the toys matrix. But rather than creating a function which I'm never going to use again, I probably would prefer to use an anonymous function and apply this to my matrix. So, I can do this by taking the same construct of what the function would be without the label, and applying it to the toys matrix. Here is the function that I would like to apply. Here is the toys matrix that I'd like to put in the data for the apply function. Then, here I'm using margin equals two, indicating that we want to apply the function to the toys, or the columns. And, here I see that the average between, say for example, Dolls, for two for Kunal, and for eight for Charles, come to an average of 10 divdided by two, which is five. The same is possible for columns, meaning toys. And I can just reverse what I had up above. For simplicity and for clarity, I am staying that we need to define our function, our anonymous function, to loop over the columns. And we have a margin set to one to indicate that we want to apply the function to the people, or the rows.


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.