R programming language is gaining much popularity in recent years due to its strong analytical tools. Loops are the most important aspects of programming as it facilitates the same job to be done until we get the result. Among these loops, For loop is most valuable as you can use it to iterate through tons of values. Using For loop in R, You can iterate over a vector, matrix, data frame, and whatnot.

This article is dedicated to showcase the working of For loop in R programming. Let’s roll!!!

The Flowchart of a For loop

The flow diagram of a For loop is given below. You will know the exact working of the for loop through this easily.

for loop in r

As you can see, the first element is input, and the For loop searches for the values in sequence. If it encountered the last value it exists the loop. If still it encountered the last value it will keep on iterating until it encounters the last value in the sequence.

1. For loop in R to display numbers

This is a very simple program for you to illustrate the working of the For loop in R.

In this example, we are going to print the numbers from 1 to 10 using the For loop.

Execute the below code to print the numbers from 1-10.

#Simple for loop in R
for (val in 1:5) 
{ 
#statement  to be printed 
    print(val) 
}  
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

The for loop iterated through all the numbers from 1 to 10 and prints them accordingly. By now, I hope you got some idea about working of for loop in R.

2. For loop in R to print the Months in year

Well, in this example, let’s use the for loop to display or print the months in a Year. Sounds interesting?? Follow me!

#For loop in R
Year<-c('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov')

#displays the months in year
for (month in Year)
{
print(month)
}
[1] "Jan"
[1] "Feb"
[1] "Mar"
[1] "Apr"
[1] "May"
[1] "Jun"
[1] "Jul"
[1] "Aug"
[1] "Sep"
[1] "Oct"
[1] "Nov"

As you can see in the above output, our for loop iterates over all the months in the year and displays each of them. You cannot even gauge the speed and power of For loop in R.

3. For loop over a list in R

Now, we can create a list in R and then try to iterate trough the list to print out all the values present in the list.

#creates a list of student credentials. 
student<-list(Name=c('Sara','Floyd','Mark','Helena'),Gender=c('Female','Male','Male','Female'),Age=c(20,21,20,22))

#prints the each name in the list
for (Name in student) 
{
print(Name)
  }
[1] "Sara"   "Floyd"  "Mark"   "Helena"
[1] "Female" "Male"   "Male"   "Female"
[1] 20 21 20 22

As the output shows, the for loop is iterating through all the values and printing or displaying all the values present in the list. Cool right?

Wrapping Up

The for loop is one of the most useful aspects of programming as it runs through all the values present in the vector, matrix, or a data frame.

This loop plays a major role in Machine learning modeling as many functions like finding missing values will make use of for loop to get the values out over a large data set.

This article provides hands-on practice for implementing the For loop in R programming. I hope now you got the better of for loops in R.

That’s all for now, Keep Looping!!!

More read: R documentation