Though R programming generally focuses on the use of vectors, it is sometimes necessary to create loops in functions. Here are some basic exercises in creating simple loops in R using repeat(), while(), for(), break(), and next statements. These are typical for use in loops so it is important to learn how they work in functions.

In a few of our examples, we will use different statements so accomplish the same tasks. It really is a matter of style, though in some instances you might find one approach works better than another.break statement. This tells R to stop processing the loop at the designated point. Try leaving break out of one of the examples and see how long you can tolerate R running in an endless loop.

The code can be found on github.

In the first example, we are going to count from 0 to 100 by four.

i <- 0
repeat{
i <- i + 4
print(i)
if(i == 100){
break
}
}
## [1] 4
## [1] 8
## [1] 12
## [1] 16
## [1] 20
## [1] 24
## [1] 28
## [1] 32
## [1] 36
## [1] 40
## [1] 44
## [1] 48
## [1] 52
## [1] 56
## [1] 60
## [1] 64
## [1] 68
## [1] 72
## [1] 76
## [1] 80
## [1] 84
## [1] 88
## [1] 92
## [1] 96
## [1] 100

Now we are going to do the same thing using the while() statement instead of repeat(). In this instance, we do not need to use the break statement. The while() command implies the process ends as long as i is less than 101.

i <- 0
while(i < 101){
print(i)
i <- i + 4
}
## [1] 0
## [1] 4
## [1] 8
## [1] 12
## [1] 16
## [1] 20
## [1] 24
## [1] 28
## [1] 32
## [1] 36
## [1] 40
## [1] 44
## [1] 48
## [1] 52
## [1] 56
## [1] 60
## [1] 64
## [1] 68
## [1] 72
## [1] 76
## [1] 80
## [1] 84
## [1] 88
## [1] 92
## [1] 96
## [1] 100

Here, we will use repeat() to run the phrase “Hello World!” ten times. Again, try it without the break statement.

phrase <- c("Hello World!")
i <- 1
repeat{
i <- i + 1
print(phrase)
if(i > 10){
break
}
}
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"

We can do the same thing with a little less coding by using while(). We don’t need to break either.

phrase <- c("Hello World!")
i <- 1
while(i < 10){
print(phrase)
i = i + 1
}
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"
## [1] "Hello World!"

Here’s a simple use of for() to pull a particular set of numbers from a sequence stored as an object.

x <- 1:100
for(i in 25:50) {
print(x[i])
}
## [1] 25
## [1] 26
## [1] 27
## [1] 28
## [1] 29
## [1] 30
## [1] 31
## [1] 32
## [1] 33
## [1] 34
## [1] 35
## [1] 36
## [1] 37
## [1] 38
## [1] 39
## [1] 40
## [1] 41
## [1] 42
## [1] 43
## [1] 44
## [1] 45
## [1] 46
## [1] 47
## [1] 48
## [1] 49
## [1] 50

This example shows how useful break can be in a slightly more complicated scenario. Here, we use break to end a series of numbers and start a new one.

for (a in 1:5){
for (b in 1:5)
{
print(c(a, b))
if( b == 3) break
}
}
## [1] 1 1
## [1] 1 2
## [1] 1 3
## [1] 2 1
## [1] 2 2
## [1] 2 3
## [1] 3 1
## [1] 3 2
## [1] 3 3
## [1] 4 1
## [1] 4 2
## [1] 4 3
## [1] 5 1
## [1] 5 2
## [1] 5 3

Finally, a function combining while() with if() and next allows us to skip a number in a sequence.

i <- 1
while(i < 10) {
i <- i + 1
if (i == 7){
next
}
print(i)
}
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 8
## [1] 9
## [1] 10

A slightly shorter piece of code using for(), if(), and next gives the same results.

i <- 1:10
for (val in i) {
if (val == 7){
next
}
print(val)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 8
## [1] 9
## [1] 10

These examples were adpted from John Akwei, “Scripting Loops in R Solutions” in the blog, R-exercises, 1 June 2016.