In this article, we will learn about For Loop in R. Loop is a rather abstract concept; it represents an action that is repeated many times and has a specific number of iterations. If you want to understand this Loop well, follow this article to the end.
What is For Loops in R?
For Loop in R is an iterative control construct that allows you to write a loop efficiently. If your Loop is a control loop, then in this Loop, we will check the condition first; if the conditions we use will be executed, the wrong ones won’t be executed.

The syntax and code example
Syntax:
for (items in condition) { # Body code }
Parameters:
- items: a variable is used to run.
- condition: a conditional expression.
Code example:
Here, we will print the numbers from zero to twelve using for loops in R. So now, we need to create a variable i run begin at i = 1 and end at i = 12 using For Loops as follows:
for (i in 0:12) { if(i%%2 == 0){ cat(i," ") }
Output
0 2 4 6 8 10 12
Another example, if you want to print out all positive numbers in a given list, we will utilize a For Loops to repeat through every one of the parts in the rundown. And afterward we will really take a look at the condition; in the event that it is a positive number, we will print it to the screen; if not, we will get to the following component in the list.
Code example:
# Create a list myList <- c(2, -5, 6, -9, 12, -55, 78) for (i in myList) { if (i > 0) { print(i) } }
Output:
[1] 2
[1] 6
[1] 12
[1] 78
Note: if you want to get out of the loop, you can use the break statement.
# Create a list myList <- c("learn", "share", "IT"); for (i in myList) { if (i == "share") { break; } else { print(i); } }
Output
[1] "learn"
Nested Loops
We can use for loop in for loop. See the example below to understand better.
Syntax:
for (i in myList) { # Body code for (j in myList) { # Body code } }
Code example:
# Create a list lstMessage <- c("Hello", "Hi") lstName <- c("Nina Lemke", "Gordon Klocko") for (i in lstMessage) { for (j in lstName) { print(paste(i, j)) } }
Output
[1] "Hello Nina Lemke"
[1] "Hello Gordon Klocko"
[1] "Hi Nina Lemke"
[1] "Hi Gordon Klocko"
Break Statement
The Break statement in R will end and exit the loop at the position of inserting the Break statement and continue to run the next statement.
Syntax:
if (test_condition) {
break
}
For example:
myList <- c(2, -5, 6, -9, 12, -55, 78) for (i in myList) { if (i == 6) { break } print(i) }
Output:
[1] 2
[1] -5
Explain:
The above code executed the Break command at “6”. Thus, when the for loop runs to “6”, the break statement will finish the loop program and continue to run the next instructions (if any).
Note: Use a break statement inside a loop (repeat, for, while) to stop iterations and control the flow of control outside the loop.
When in a nested loop situation where there is one loop inside another, this break statement exits the innermost loop being evaluated.
Next Statement
With the Next statement, the loop program will not terminate. When running the program, the iterator will skip the iteration at the position where the Next statement was inserted and continue at the next iteration.
Syntax:
if (test_condition) {
next
}
For example:
myList <- c(2, -5, 6, -9, 12, -55, 78) for (i in myList) { if (i == 6) { next } print(i) }
Output:
[1] 2
[1] -5
[1] -9
[1] 12
[1] -55
[1] 78
Explain:
When the loop runs to the Next statement, the program will automatically skip the iteration at this position and continue to loop in the next time.
Combine If…Else in For loop
When building a For loop program, users can completely combine the If…else statement in the loop. Combining the if…else statement makes it possible to nest the condition of the statement inside the loop.
For example:
ourList <- 1:6 for(i in ourList) { if (i == 1) { print(paste("Your number is", i, "Jump!")) } else { print(paste("Your number is", i, "Go!")) } }
Output:
[1] "Your number is 1 Jump!"
[1] "Your number is 2 Go!"
[1] "Your number is 3 Go!"
[1] "Your number is 4 Go!"
[1] "Your number is 5 Go!"
[1] "Your number is 6 Go!"
Explain:
The above example is built for a game, the If statement will evaluate the loop and print the result. When the loop reaches the value 1, it will print “Jump!”. When iterating to the remaining numbers, the program will print “Go!”
For Loop with Matrix
In R, a matrix is a two-dimensional data structure, to use the For loop into a matrix, the user will apply a nested for loop.
For example:
myMatrix <- matrix(1:6, nrow=3, ncol=2) for (row in 1:nrow(myMatrix)) { for (col in 1:ncol(myMatrix)) { print(paste('Row:', row, 'col:', col, 'value:', myMatrix[row, col])) } }
Output:
[1] "Row: 1 col: 1 value: 1"
[1] "Row: 1 col: 2 value: 4"
[1] "Row: 2 col: 1 value: 2"
[1] "Row: 2 col: 2 value: 5"
[1] "Row: 3 col: 1 value: 3"
[1] "Row: 3 col: 2 value: 6"
For Loop with DataFrame
Similarly, users can absolutely use a For loop with DataFrame. Its operation is similar to that of the Matrix.
For example:
myClass <- data.frame(gender=c('boy', 'girl'), quantity=c(26, 25), avg_age=c(22, 20)) print(myClass) for (row in 1:nrow(myClass)) { for (col in 1:ncol(myClass)) { print(paste('Row:', row, 'col:', col, 'value:', myClass[row, col])) } }
Output:
gender quantity avg_age
1 boy 26 22
2 girl 25 20
[1] "Row: 1 col: 1 value: boy"
[1] "Row: 1 col: 2 value: 26"
[1] "Row: 1 col: 3 value: 22"
[1] "Row: 2 col: 1 value: girl"
[1] "Row: 2 col: 2 value: 25"
[1] "Row: 2 col: 3 value: 20"
Summary
Above is all I show you about the syntax and usage of for loops in R. If you have any questions, please leave a comment below and I will answer your questions.
Have a good day!
Maybe you are interested:
- Sum Function In R: How To Use Sum() In R
- Merge multiple data frames in r
- Funs In R: What Does Funs Mean In R And Code Examples

Hi, guys! My name’s Scott Miller. My current job is a software developer and I have shared a lot of quality articles about Javascript, C, C++, C#, Python, PHP, R, Java programming languages. I’m hoping they can assist you.
Name of the university: HCMUS
Major: IT
Programming Languages: C, C++, Python, R, Java, JavaScript