Loops are the most powerful tools of the R programming language. This comprehensive guide includes the types of loops in R – for, while, repeat. Find out how to use loops in R.
In this guide, we will work on three loops in R – for, while, repeat.
- for-Loop: Loop is finite. The total number of iteration needs to be specified in for-loop.
- while-Loop: Loop is infinite, but it needs logical condition in while-loop to stop iteration.
- repeat-Loop: Loop is infinite, but it needs logical condition to stop iteration. Logical condition needs to specified at the end of code block with if-break couple.
![Flowchart of Loops in R - For, While, Repeat Flowchart of Loops in R - For, While, Repeat](https://universeofdatascience.com/wp-content/uploads/2021/06/Loops_in_R-1024x576.png)
Check Out: 6 Ways of Subsetting Data in R
for-Loop in R
A for-loop includes two parts. First, a header specifies the number of iteration. Second, a body containing a code block executed once for each iteration. In a for-loop, loop is finite. The total number of iteration is specified in header part. In our example, we write “Welcome to Universe of Data Science” text five times. Therefore, we need to specify five in header part.
for (i in 1:5){
print("Welcome to Universe of Data Science")
}
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
Also Check: How to Clean Data in R
while-Loop in R
A while-loop is used when we don’t know the exact number of times our R code needs to be executed. Loop is infinite, but it needs logical condition to stop iteration. A while-loop includes two parts. First, a header specifies the logical condition to stop iteration. Second, a body containing a code block executed once for each iteration. In our example, we write “Welcome to Universe of Data Science” text five times. Therefore, we specify the condition as five in header part.
i <- 1
while (i<=5){
print("Welcome to Universe of Data Science")
i <- i+1
}
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
Also Check: How to Handle Missing Values in R
repeat-Loop in R
Like while-loop, a repeat-loop is used when we don’t know the exact number of times our R code needs to be executed. Loop is infinite, but it needs logical condition to stop iteration. Logical condition needs to specified at the end of code block with if-break couple. In our example, we write “Welcome to Universe of Data Science” text five times. Therefore, we specify the condition as five in if code.
i <- 1
repeat{
print("Welcome to Universe of Data Science")
i <- i+1
if(i>5) break
}
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
## [1] "Welcome to Universe of Data Science"
The application of the codes is available in our youtube channel below.
Don’t forget to check: How to Categorize Numeric Variables in R
![](https://universeofdatascience.com/wp-content/uploads/2023/11/OD-3.jpeg)