Control Structures in R

This tutorial looks at some of the common control structures in R. Control Structures define the flow of the program and decides which path the program takes. This decision is based on the evaluation of a variable. These are the basic types of control structures:


  • if a variable satisfies a particular condition follow path A , otherwise follow path B.(if, if-else, ifelse)
  • Continue evaluation a body of expressions till a specified condition is met. This is a looping construct and there are multiple ways to specify termination condition.(for, repeat, while and related next, break.).
  • Select a block of code for evaluation depending on the value of a variable (switch)
  • R has overloaded certain operators so that they internally work on a looped structure. For example if you use a ‘+’ to add to arrays then R adds individual elements of the arrays.
  • R has certain convenience or helper functions to ‘apply’ a function to multiple elements of a list (apply, lapply, sapply, vapply, replicate, tapply, by ). These will be covered in the next tutorial.

if, if-else, ifelse

if: Lets look at an example

There is an easier way to do an if else

The ifelse function is read like this: evaluate the expression specified in the first argument, if it is true return the result of the second argument, else return the result of the third argument.

for and while

The for construct can be used to loop over values of a variable.

In the example above we iterate over the values of the list d. The list contains three element and each iteration the variable v is assigned the value of d during that iteration. Here, v is first assigned a value of 1 and then 2 and 3. During each loop we just print the value of v in that iteration.

It is possible to iterate over a matrix too

Iterating over a sequence of numbers

Lets see how iterating over a data frame looks like. In the loop we will not print the variable but will look at its structure

so each iteration gets a column. If you now want to iterate over the column then you use a
nested loop

so thats how you iterate over the data frame column first. How about row first? well, you can use the ‘apply’ or the ‘by’ function. We will look at them in the next tutorial.

We now look at the while loop.

This is how it works. We initialize a to 0 and b to 0. The control enters the while loop and checks the value of a. The first time it is less than 10 so it enters the body, gets incremented by 1 and then the control returns to the expression inside while loop. At this point a =1, but its still less than 10 so the whole thing continues. Once a becomes 10 the while expression evaluates to false and then it leaves the body.

Be careful of the while loop. The expression in the while bracket has to evaluate to false at some point, otherwise the code just hangs.

next, break and repeat

The next command is used to stop executing the current body of the loop and go to the next iteration (if any are left). Here’s an example of how we can add even numbers and print odd ones. Note that the example is just to demonstrate the use of next and is not the best way to add even numbers.

This is how it works, when the number is even (modulus is 0) the control enters the body of if loop, increments sum by i and then exits the iteration. The next iteration then starts where i is odd. The control here does not enter the if body but prints i and then the iteration is complete

To explain the break statement lets just continue using the above iteration and say that we want to stop doing everything if the sum becomes greater than 20.

In the above example when the sum becomes greater than 10 (12), we want to stop processing the get out of the for loop. the break statement does just that.

It now seems such a waste to specify an upper limit to i. We can use the repeat keyword instead,

The repeat function can be used to continue looping over a body. Remember to put in a condition of exit (break), otherwise the loop loops forever


We end this tutorial with a look at the switch function. Lets look at an example

The function works like this: the first argument in the switch statement is evaluated. If it evaluates to 1 the first argument after the expression is evaluated (“a”) and so on. The expression can also evaluate to a character (and not a factor, in which case it is coerced to integer.)

This completes our tutorial on the control structures, in the next tutorial lets look at the “apply” group of functions.

Leave a Reply

Your email address will not be published. Required fields are marked *