Iterating over a range in R – For loop
# R Program to demonstrate
# the use of for loop
for (i in 1: 4)
{
print(i ^ 2)
}
Output:
[1] 1
[1] 4
[1] 9
[1] 16
In the above example, we iterated over the range 1 to 4 which was our vector. Now there can be several variations of this general for loop. Instead of using a sequence 1:5, we can use the concatenate function as well.
Using concatenate function in R – For loop
for (i in c(-8, 9, 11, 45))
{
print(i)
}
Output:
[1] -8
[1] 9
[1] 11
[1] 45
Instead of writing our vector inside the loop, we can also define it beforehand.
Using concatenate outside the loop R – For loop
x <- c(-8, 9, 11, 45)
for (i in x)
{
print(i)
}
Output:
[1] -8
[1] 9
[1] 11
[1] 45
Nested For-loop in R
R programming language allows using one loop inside another loop. In loop nesting, we can put any type of loop inside of any other type of loop. For example, a for loop can be inside a while loop or vice versa. The following section shows an example to illustrate the concept:
Example:
for (i in 1:3)
{
for (j in 1:i)
{
print(i * j)
}
}
Output:
[1] 1
[1] 2
[1] 4
[1] 3
[1] 6
[1] 9
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.
While Loops
The while loop, set in the middle of the figure above, is made of an initialization block as before, followed by a logical condition.
# Your User Defined Function
readinteger <- function(){
n <- readline(prompt="Please, enter your ANSWER: ")
}
response <- as.integer(readinteger())
while (response!=42) {
print("Sorry, the answer to whatever the question MUST be 42");
response <- as.integer(readinteger());
}
Comments