R
No Comments

Beginners guide to write for loop in R

In any programming language, loops are used to repeat a set of instructions. In this post, we are going to learn about for loop in R language.

It is used to perform an iteration over a sequence, like a vector or a matrix, based on some condition. An iteration means a single unit of task within a loop.

A for loop executes for each element within a sequence. The code present in the body is executed until the end of the sequence is reached. The execution stops after the last element of the sequence is processed. So the number of times a for loop gets executed is equal to the number of elements that is sent as input.

image of for loop in R
Flowchart : for loop in R

Please note that a for loop is generally not necessary in R programming, because the R language supports vectorization. Code that uses apply functions, like lapply and sapply, on vectors produce faster calculations.

Let us understand how a R for loop can be written, using the below examples.

For loop over a vector in R

Let us create a vector of numbers

You can do a for loop on a vector in two different ways.

  • By creating a temporary variable using the in keyword
for loop on vector approach 1
for loop on vector approach 1
  • Using the length method.We loop through every item in the vector using its index,and then return a number on each line, using the print function.
for loop on vector approach 2
for loop on vector approach 2

for loop over lists in R

Let’s create a list and use a for loop to print each item of that list.

for loop over list
for loop on list

Looping over matrix in R

Let’s create a matrix and loop through it.

Nested for loop in R

Let’s use a nested for loop to print each item of a matrix along with its row and column location.

nested for loop in r
nested for loop example

How to write for loop on R dataframe

Lets create a R dataframe first.

for loop on R dataframe
create dataframe in R

Now print each column of this dataframe by using a for loop.

for loop on dataframe in R

for loop in R with increment

This is how you can structure a for loop so that it will increment the counter by 4, which is called the step size .

 

for loop with increment
for loop using a stepsize

Note that you can execute the same logic of a step size without using for loop as well.

 

When to use next statement in for loop

The next statement should be used when you want to skip execution of the loop based on whether a particular condition is true, without stopping the execution of the entire loop.

In the example given below, I want to print all element other than 30. So I will use next statement when the element is 30.

All the statement below the next statement would not get executed when the if condition is true.

next statement in for loop

Hope this article gave you a good understanding on writing for loops in R for different scenarios. Please let me know your feedback on the article below.

Arm yourself with the most practical data science knowledge available today.

KEEP LEARNING

Menu