Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In programming, loops are used to repeat the execution of a block of code. Loops help you to save time, avoid repeatable blocks of code, and write cleaner code.
In R, there are three types of loops:
- while loops
- for loops
- repeat loops
R for Loop
A for
loop is used to iterate over a list, vector or any other object of elements. The syntax of for
loop is:
for (value in sequence) { # block of code }
Here, sequence is an object of elements and value takes in each of those elements. In each iteration, the block of code is executed. For example,
numbers = c(1, 2, 3, 4, 5) # for loop to print all elements in numbers for (x in numbers) { print(x) }
Output
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5
In this program, we have used a for
loop to iterate through a sequence of numbers called numbers. In each iteration, the variable x stores the element from the sequence and the block of code is executed.
Example 1: Count the Number of Even Numbers
Let’s use a for
loop to count the number of even numbers stored inside a vector of numbers.
# vector of numbers num = c(2, 3, 12, 14, 5, 19, 23, 64) # variable to store the count of even numbers count = 0 # for loop to count even numbers for (i in num) { # check if i is even if (i %% 2 == 0) { count = count + 1 } } print(count)
Output
[1] 4
In this program, we have used a for
loop to count the number of even numbers in the num vector. Here is how this program works:
- We first initialized the count variable to 0. We use this variable to store the count of even numbers in the num vector.
- We then use a
for
loop to iterate through the num vector using the variable i.
for (i in num) { # code block }
- Inside the
for
loop, we check if each element is divisible by 2 or not. If yes, then we increment count by 1.
if (i %% 2 == 0) { count = count + 1 }
Example 2: for Loop With break Statement
You can use the break
statement to exit from the for
loop in any iteration. For example,
# vector of numbers numbers = c(2, 3, 12, 14, 5, 19, 23, 64) # for loop with break for (i in numbers) { # break the loop if number is 5 if( i == 5) { break } print(i) }
Output
[1] 2 [1] 3 [1] 12 [1] 14
Here, we have used an if
statement inside the for
loop. If the current element is equal to 5
, we break the loop using the break
statement. After this, no iteration will be executed.
Example 3: for Loop With next Statement
Instead of terminating the loop, you can skip an iteration using the next
statement. For example,
# vector of numbers numbers = c(2, 3, 12, 14, 5, 19, 23, 64) # for loop with next for (i in numbers) { # use next to skip odd numbers if( i %% 2 != 0) { next } print(i) }
Output
[1] 2 [1] 12 [1] 14 [1] 64
Here, we have used an if
statement inside the for
loop to check for odd numbers. If the number is odd, we skip the iteration using the next
statement and print only even numbers.
Nested for Loops
You can include a for
loop inside another for
loop to create a nested loop.
Consider the example below. Suppose we have two sequences of numbers. We want to print all the combinations where the sum of numbers in both the sequences is even.
# vector of numbers sequence_1 = c(1, 2, 3) sequence_2 = c(1, 2, 3) # nested for loop for (i in sequence_1) { for (j in sequence_2) { # check if sum is even if ( (i+j) %% 2 == 0 ) { print(paste(i, j)) } } }
Output
[1] "1 1" [1] "1 3" [1] "2 2" [1] "3 1" [1] "3 3"
In the above program, we have created two sequences: sequence_1 and sequence_2, both containing numbers from 1
to 3
.
We then used a nested for
loop to iterate through the sequences. The outer loop iterates through sequence_1 and the inner loop iterates through sequence_2.
for (i in sequence_1) { for (j in sequence_2) { # code block } }
In each iteration,
- i stores the current number of sequence_1
- j stores the current number of sequence_2
The if
statement inside the nested loops checks if i + j
is even or not. If it is, then we print i and j.
if ( (i+j) %% 2 == 0 ) { print(paste(i, j)) }
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.