![]() |
Home |
---|
A for loop is used to repeat a block of code. In this article you will learn how to create a for loop in R programming with examples & exercises for practice.
Loops are iterative structures which repeat a statement or a block of code - a sequence of instructions depending on certain conditions. A for loop is initialized at the beginning and a condition is checked if the test expression evaluates to TRUE the block or body of loop is executed. The body of loop may contain one statement to as many statements as required. After each run of loop the condition is checked again. In case if the condition is FALSE the control sequence moves out of the body of loop to next statement just after loop.
for ( value in sequence){
body
}
Usually it is used in R to iterate over a vector. Here the loop iterates for each item in the list and its code is executed when it reaches the end of list it exits.
for ( x in 1:10){
print(x)
}
This code will print numbers from 1 to 10. Now lets write this code in Rstudio and see the output practically.
Another interesting example will be writing times table of 3. Here what we want R programming to do for us is to write times table from 1 to 10. The R code for this is
for (y in 1:10){
print(paste(3, '*' , y , '=' , 3*y))}
In rstudio the output is
Here a new
function paste is used. This function is used to concatenate strings
and other objects.
You can nest for loops in R for complex tasks. A matrix usually consists of rows and columns and to output the items in a matrix you require two loops one for row and one for column. Here nesting is used to print the row number and column number.
Lets suppose the matrix has 3 rows and 4 columns.
for ( row in 1:3 ) {
for ( col in 1:4 ) {
print ( paste ( row, col ) )
}
}
And the output will be
[1] "1 1"
[1] "1 2"
[1] "1 3"
[1] "1 4"
[1] "2 1"
[1] "2 2"
[1] "2 3"
[1] "2 4"
[1] "3 1"
[1] "3 2"
[1] "3 3"
[1] "3 4"
In R next skips or discontinues a specific cycle or iteration and jumps to the test condition. In other programming languages like C, C++ and java a word 'continue' is used but in R a better described word 'next' is used which means skip this cycle of loop and go to test condition to see if loop will continue or not.
Here is an example. Lets suppose you want to write even numbers from 1 to 20. First you write a program to write all numbers from 1 to 20.
for ( x in 1:20 ) {
print ( x )
}
This program will print numbers from 1 to 20.Now you will change this program to write all even numbers from 1 to 20 with the help of next statment
for ( x in 1:20) {
if ( x %% 2 )
next
print(x)
}
Here a test is placed with if statement. %% is modulus operator which returns the remainder of a division. If x is divided by 2 and remainder is zero, the expression will result into a FALSE as a result 'next' statement will not be executed and print function will print x which will be an even number. In case x is divided by 2 and remainder is 1, it will be TRUE for if statement, as a result the 'next' statement will be executed and instead of going to print function the loop will jump to next iteration of the loop.
When 'break' statement is used in a loop it will pass the program control to the code after the body of loop or in simple words it will break out of the loop or exit the body of loop in which this break statement is written.
Lets see an example
limit = 20
for ( counter in 1:30 ) {
if(counter == limit)
break
print(counter)
}
When counter will reach 20 which is the limit, the if condition will become true and break will be executed which will make control jump out of this structure and remaining iterations will be skipped.
1. Print "R Programming" 5 times
2. Print counting from 25 to 35
3. Print times table of 5 from 1 to 20
4. Write a program to write row and column items of a matrix
5. Write a program to write from 1 to 30 with the condition that it skips all the multiples of 3 or simply it writes like 1,2,4,5,7,8,10,11,.....29
I hope that after this simple and short R iterative structure tutorial you have learnt the basics of for loop in R Programming and now you can write it yourself. Happy learning R for statistics and data science!Multiplication Table in R with For Loop Explanation
Find Factorial of a number in R with For Loop