Repetitively Apply Actions in R
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. 


[Instructor] In order to repetitively apply actions in R, we can utilise the Apply function. There are many Apply functions. In this case here, we're using the Apply function which allows you to essentially loop over and run multiple actions on multiple chunks of data. However less code is required than loop, and it's technically coded in a faster way. Let me a use case of toys that I have purchased with my friends. Here are the costings of them. And we'd like to say, for example, summarise the costs of these children's toy by finding the sum of each row. Now we could do this manually by taking the sum of each row one by one and then asking for the sum of each of these. I could have used a for loop to iterate over from one to three and printed out the output of the sum of each of my different rows. But it may be a faster way to do this would be to use the Apply function which returns the sum of each as we have seen as asked three outputs for each of our sum of our three rows. But the amount of code used is a lot less, and the speed which which we received the answer was a lot faster. 

In order to understand the Apply function, let's call the Help to the screen using the question mark. Which then brings up the Help tab with the R documentation for the base function called the Apply. And here we see the description of this function and the three parameters that I used. We can note through reading the Arguments section that the X is an array or a matrix. This means that the data that you will be performing the function on, in our case, we needed to understand toys. You could have Margin which would take arguments of either one or two. In this case I chose one because I was looking at each row one by one. Had I wanted to look at each column then I could have used the margin two. And the fun stands for function which is to be applied to your dataset for said margin that you're looking at. Let's have a look at trying to use the Apply function with various different base functions. So if I wanted to know how many data points I had in each row, I could use the margin one for by row. Here we can see that it says, for example, a matrix one indicates rows in the Help window. I'm using the builtin function length, and I'm using the dataset toys. And if I bring toys up onto the screen, we can see there are four entries for each of my different rows. I could ask for the standard deviation of the rows by using the builtin sd function. I could ask for the average by column to understand how much each toy might cost on average. And for the sake of fun, I could shuffle the toys between. And in this case I've chosen two which would be the columns, so I've chosen within each column to shuffle them around, but I'm not moving things between rows.


About the Author
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.