How to Write a Loop in R

102

In this R tutorial, we will discuss how to write a Loop. Looping statements are used to iterate the elements present in the given data structure or a given range of numbers. We will discuss for loop in R.

How to Write a Loop in R

Syntax:

for (iterator in data/sequence)
{
statements
----------
}

Data is a data structure like a vector, represents a sequence of numbers, and an iterator is a variable used to iterate the data or sequence.

Also ReadHow to Merge Data in R

And statements are used to execute inside for loop.

Example 1: We will create a sequence of numbers from 1 to 10 and display using for loop.

for (i in 1:10){
print(i)
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

We can also perform some operations inside an iteration.

Also ReadMatrices in R | How to Create, Update, and Modify R Matrices

Example 2: Sum all the elements

sum=0
for (i in 1:10){
sum=sum+i
}
print(sum)

Output:

[1] 55

Example 3: iterate through a vector and list

#create a vector
vector1=c(1:10)

#iterator through a vector
for (i in vector1){
print(i)
}

#create a list
list1=list("php","c","cpp","java","jsp")

#iterator through a vector
for (i in vector1){
print(i)
}

#iterator through a list
for (i in list1){
print(i)
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] "php"
[1] "c"
[1] "cpp"
[1] "java"
[1] "jsp"

We can also specify conditions inside for loop.

Also Read10 Best R Programming Courses On Udemy

Example:

Iterate elements from 1 to 10, and if the values are more significant than 5, print greater than 5.

#create a vector
vector1=c(1:10)

#iterator through a vector
for (i in vector1){

#condition greater than 5
if(i<=5){
print(i)}else{
print("greater than 5")
}
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] "greater than 5"
[1] "greater than 5"
[1] "greater than 5"
[1] "greater than 5"
[1] "greater than 5"

We can control the looping statements using the break keyword through conditional statements. It will terminate the loop.

Also ReadR If Elseif Tutorial With Examples

Syntax:

for (iterator in data/sequence) 
{ 
conditional statements 
----------
break
----------
other statements
----------
}

Example:

In this example, we will iterate 10 elements, and if the element is equal to 5, we will stop the iteration using the break keyword.

#create a vector
vector1=c(1:10)

#iterator through a vector
for (i in vector1){

#break when element is equal to 5
if(i==5){
break
#display
}
print(i)
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4

Also Read10 Best Data Science Coursera Courses For Beginners

next

We can also control the looping statements using the next keyword through conditional statements. It will terminate the loop at a particular condition and starts looping from the next iterating element.

Syntax:

for (iterator in data/sequence) 
{ 
conditional statements 
----------
next
----------
other statements
----------
}

Example:

In this example, we will iterate 10 elements, and if the element is equal to 5, we will stop the iteration and continue again using next.

#create a vector
vector1=c(1:10)

#iterator through a vector
for (i in vector1){

# when element is equal to 5 stop the statement and continue using next
if(i==5){
next
#display
}
print(i)
}

Output:

[1] 1
[1] 2
[1] 3
[1] 4
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Summary

From this tutorial, we discussed for loop in R along with control statements in a loop.

Other related articles: