For Loops

Loops allow us to iterate over a sequence of items, facilitating operations such as summing a list of values, modifying a list of names, or conducting searches in a specific manner. In the Tidyverse ecosystem, much of the looping is abstracted away through the use of column-wise mutations on dataframes, which simplifies many operations that would traditionally require loops. Despite this, understanding loops remains a fundamental aspect of programming in R, where you might find for loops to be indispensable. R offers three types of loops: for, while, and repeat. This lesson focuses on the for loop, while the next lesson will introduce the while loop.

A for loop sequentially iterates over each element in a specified sequence.

During each iteration of a for loop, a loop variable (also known as an iterator) is automatically assigned the value of the current item in the sequence. The loop continues until every item in the sequence has been used. Here is the basic syntax for a for loop:

We can use a for loop with any object that can be sequenced, including lists, vectors, and dataframes. The loop is initiated with the for keyword, followed by the specification of the iterator - in this case i - and the sequence to iterate over - in this case sequence - with code to run for each iteration within curly brackets. Following this, indented lines of code delineate the actions to be performed during each iteration, utilizing the current value of the iterator. The loop cycles through each element in the sequence, executing the specified actions each time, and concludes when there are no more elements to process. Now let's see this in action:

Or if we would like to iterate over a range of integers we may more succinctly use the colan in between to numbers. For example 1:3 will make a vector of integers from 1 through 3, which is the same as writing c(1,2,3), or 1:100 would be the same as c(1,2,3,...,98,99,100)

Skipping and exiting loops

While iterating over a sequence in a loop, there may be instances where we wish to skip certain values or exit the loop before it naturally concludes. The next statement allows us to skip specific iterations, often employed in tandem with a conditional statement to dictate which elements should be bypassed. Whenever the next statement is executed, the loop foregoes the remaining lines in the current iteration and returns to the beginning to start the next iteration. For instance, in the example we'll look at, the for loop will skip the rest of the commands for the current iteration when val > price_limit and commence the next cycle.

On the other hand, if the intention is to exit the loop prematurely, this can be achieved using the break command. Like the next command, it is often utilized alongside a conditional statement to determine the specific scenario where the loop should terminate. When the break statement is encountered, the loop ends instantly, and the program moves to execute the line of code outside of the loop. In our forthcoming example, the for loop will terminate altogether when the condition sum(purchasable) >= purchase_limit is met, avoiding any further iterations.