Understanding Loop expressions in R

Wind wheel, for computing wind force, by Jules Ric

when starting to use R, most R users use loops whenever they need to iterate over elements of vector, list or data frame. For e.g. if we are interested in the mean of all the numeric columns of the built-in iris data set, we could type, four times mean(iris$Sepal.Length), with each input variable name changing each time.

head(iris)

Iris data set

However, a far more efficient way to calculate mean of all numeric variables in iris data is to use the for loop.

The loop constructs are used to execute repetitive code statements for a particular number of times.

There are two fundamental loop constructs in R:

  • For loop
  • While loop

In this article we will learn for and while loop constructs with simple examples. Let’s get started.

The general syntax of for loop is provided below.

for loop construct

x is counter and as x assumes each sequential value defined by numeric vector numbers the code in the body will be repeated up-to the last index position of numeric vector.

numbers <- c(10,20,30,40)
for (x in numbers){
  print(x)
}

[1] 10
[1] 20
[1] 30
[1] 40

Because first value in numbers is 10, the loop starts by replacing x with 10 and runs everything between curly braces. The x acts as a counter for the loop, the name of the counter does not matter here, you are free to use whatever the name you like. Once the first iteration is complete, the for loop loops back to the beginning and replaces x with the next value in numbers vector (20 in this case). This process is then repeated until the loop reaches the final value in numbers vector (40 in this example) after which the loop exits.

Let’s use the for loop to calculate mean of all the numeric column of iris data set.

The function seq_along() is very helpful for the for loops, because it automatically moves along the number of columns of the data frame. We will use this function to generate for loop iterations.

for(i in seq_along(iris)){
  print(mean(iris[[i]]))
}

[1] 5.843333
[1] 3.057333
[1] 3.758
[1] 1.199333
Warning: argument is not numeric or logical: returning NA[1] NA

Accessing the column is done using indexing, so when i=1, iris[[i]] is equal to the Sepal.Length variable, which is column 1, and so on. We got an error for column 5, because it is not numeric (the Species variable is), hence a mean value is not possible.

This is a great example of where we can combine for loops with an if statement. Take a look at the following code.

for(i in seq_along(iris)){
  if(class(iris[[i]])=="numeric"){
  print(mean(iris[[i]]))  
  }
 }
 
[1] 5.843333
[1] 3.057333
[1] 3.758
[1] 1.199333

The if statement will print the mean of iris column if the class of that column is numeric.

While loops begin by testing a condition. If the condition is TRUE, then they execute the statement. The while loop continues to run until condition is FALSE, after which the loop exits.

A typical while loop construct looks as follows:

while loop construct

Here the action will only occur if test expression is TRUE. Otherwise, R will not enter the curly braces and run what’s inside them.

i = 0
while(i <= 3){
  print(paste("loop",i))
  i = i + 1
}

[1] "loop 0"
[1] "loop 1"
[1] "loop 2"
[1] "loop 3"

Because we set our test expression to be i less than or equal to 3, the loop stopped printing once i was 4, and R broke out of the while loop.

While loops can potentially result in infinite loop if not written properly, therefor you must use them with care. Here is an example of infinite while loop. Press Esc to stop the loop.

while(TRUE){
  print("yes!")
  }

You can prevent the infinite while loop using break statement. The loop will run for five iterations and will jump out of the while loop due to break statement.

i = 0
while(TRUE){
  print("yes!")
  i = i+1
  if(i==5){break} # break statement will exit the loop immediately
}

The primary difference between for loop and while loop is: a for loop is used when the number of iterations a code should be run is known where a while loop is used when the number of iterations is not known.

For loop walks though the iterator(usually, this is sequence of numbers), a while loop will not iterate through the sequence of numbers. Instead, it requires a condition, and action will only occur if the condition is TRUE.

for loop uses loop iterator to iterate the block of code. while loop uses condition to execute the block of code.
Both for and while loops are used to execute the repetitive code statements.

Leave a Comment

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

Scroll to Top