Loops in R and alternatives – A Quick Tutorial

In this tutorial we’ll learn how to write Loops in R.  Like if-else statements, loops are commonly used in data science related programming. We’ll start with an introduction to loops and then cover the various types of loops – the for loop, the while loop and the repeat loop. 

Loops are used when we need to execute a block of code multiple times (these are called iterations). The statements inside a loop are executed if a condition is true. If a condition is false, then the loop is exited.

Loops in r flowchart

Types of Loop

We use the for loop when you know the number of iterations in advance, while we execute the other two loops, repeat and while, based on conditions that are evaluated as TRUE or FALSE. In the while loop, we check the condition at the beginning of each iteration, whereas in the repeat Loop, we check the condition at the end of each iteration.

Types of loops in R

For Loop in R

Let’s start with the for loop in R.  Iterations are performed using a Loop Variable which takes a start value and an end value as defined in Sequence.  The statements form the body of the loop and are executed each time until the Loop Variable exceeds the maximum value defined in Sequence.

 Syntax:
 
 for  (Loop_Variable  in  Sequence) {
            Statement 1
            Statement 2
            ....
  }                  

In this example,  i is the Loop Variable,  which can take values 1 to 4. Therefore, the loop is executed 4 times. Each time the value of i is printed. Here the number of iterations are fixed in advance as 4 iterations.

 [1] 1
 [1] 2
 [1] 3
 [1] 4 

In this example, there are 6 iterations in the loop. The Loop Variable takes values as per the vector Location. The counter object count is used to obtain the number of instances of “Mumbai”. The if statement checks for Mumbai in Location and increases the count by 1. The Count of Mumbai is subtracted from the length of Location, giving the count of Delhi.

 Location<-c("Mumbai","Delhi","Delhi","Mumbai","Mumbai","Delhi")
 count <- 0
 for (i in Location) {
   if(i=="Mumbai")  
     count=count+1
 }
 countdelhi<-length(Location)-count
 print(paste("Mumbai:",count))
 print(paste("Delhi:",countdelhi)) 
 [1] "Mumbai: 3"
 [1] "Delhi: 3" 

Nested for Loops

The placing of one loop inside the body of another loop is called nesting.  When you “nest” two loops, the outer loop takes control of the number of complete repetitions of the inner loop. Therefore, for each iteration of the outer loop, the inner loop is executed a specified number of times. In the example, for each of 5 iterations of the outer loop, the inner loop is executed 2 times. The product of two loop variables is printed.

# Print numbers :

 for(i in 1:5){
   for(j in 1:2)
 {
   print(i*j);
 }
 } 
 [1] 1
 [1] 2
 [1] 2
 [1] 4
 [1] 3
 [1] 6
 [1] 4
 [1] 8
 [1] 5
 [1] 10 

In general, nested loops have two loop variables. The corresponding sequence defines the number of iterations of each loop.

 Syntax:
 
  for (Loop_Variable1 in Sequence1){
      Statement1
   for(Loop_Variable2 in Sequence2){
            Statement2
            Statement3
        }
 } 
nested for loop

While writing a nested for loop, always practice indentation. Like other languages, R does not create an indentation automatically, so when you write a loop inside another loop, always indent before writing a new loop so that it’s easy to see and understand the code.  It also makes it easier to understand the  placing of loops and where the brackets and close. 

As we discussed, the for loop is preferred when the number of iterations is known in advance. However, the number of executions is often determined by some condition. In such situations, while or repeat loops can be used.

The while loop

The while loop has a condition which is evaluated at the beginning.  If the condition is TRUE, then all statements which form body of the loop are executed.

  Syntax:
 
   while  (condition) {
            Statement 1
            Statement 2
      } 
while loops

In our example, the condition is i is less than 10. The initial value of i is one.  The condition is TRUE, and hence i will be printed. Further, i is incremented  by 2.  The while loop is executed again as i=3 implies condition is TRUE.  The loop is executed 5 times and stops when i takes a value of 11.

 i<-1
 while (i<10) {
   print(i)
   i=i+2
 } 
 [1] 1
 [1] 3
 [1] 5
 [1] 7
 [1] 9 

In this example, the number is entered using the readline function in R. The number should be an integer. If the number is less than zero, then the message

“Enter a positive number” is printed.  Assume that the number entered is 3. In this case the while loop is executed, the sum becomes 3 and the number 2.

The body of the while loop is executed again, the sum becomes 5 and the number 1. Finally, the sum becomes 6 and the number zero. This stops the while loop.

 number=as.integer(readline(prompt="Enter a number: "))
 if(number < 0) {
   print("Enter a positive number")
 } else {
   sum=0
   while(number > 0) {
     sum=sum+number
     number = number - 1
   }
   print(paste("The sum is", sum))
 } 

The repeat loop

This loop is similar to the  while loop, but it is used when we want blocks of statements to be executed at least once, no matter what the result of the condition.

The condition is evaluated inside the repeat loop.

This is the general syntax of the repeat loop. The condition is evaluated inside the loop and a break statement is used to terminate the loop if the condition is TRUE.

    Syntax:
 
      repeat {
         Statement 1
         Statement 2
        if (condition) {
   break
         }
       } 

Here the initial value of the total is zero. The repeat loop starts and total takes a value= 2.  If the condition total greater than 8 is TRUE, then the loop is terminated Using the  break statement.

 total<-0
 repeat { 
   total<-total+2 
   print(total) 
   if(total > 8) 
     break
 } 
 [1] 2
 [1] 4
 [1] 6
 [1] 8
 [1] 10 

Interruption and exit statements

The break and next statements are used to change the natural behavior of a loop. The break statement ends the loop and passes control to the statement immediately after the end of the loop (if any). The next statement discontinues the current iteration of the loop without terminating it and starts next iteration of the loop

interrupt or exit statements

Here, the if statement checks whether the value in i is divisible by 4 or not; if it returns TRUE,  then it skips all the statements after that and starts the next iteration of the loop.

 for (i in 1:15) {
      if (i%%4){
        next
      }
      print(i)
    } 

This is a quick summary of concepts here.  We covered the for loop, while loop and repeat loop. This tutorial is based on lessons from the Data Analytics in R unit of the Digita Schools Advanced Diploma in Data Analytics.

loops in r summary

This tutorial lesson is taken from Digita Schools Advanced Diploma in Data Analytics and the Postgraduate Diploma in Data Science.

You can try our courses for free to learn more