Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Loops

R Loops

Sometimes we may need to execute the same block of code multiple times. Normally, statements are executed sequentially: the first statement in a function executes first, followed by the second, and so on.

Programming languages provide various control structures for more complex execution paths.

Loop statements allow us to execute a statement or group of statements multiple times. The following is a flowchart of a loop statement in most programming languages:

The loop types provided by R are:

The loop control statements provided by R are:

Loop control statements change the execution order of your code, allowing you to jump to different parts of the code.

Loop types

repeat

A repeat loop keeps executing code until a conditional statement is true; to exit, you use the break statement.

The syntax format is as follows:

repeat { 

    // Related code

    if(condition) {

       break

    }

}

The following example exits the loop when the variable cnt is 5; cnt is a counter variable:

Example

v

<-

c

(

"Google"

,

"Runoob"

)

cnt

<-

2

repeat

{

   

print

(

v

)

   cnt

<-

cnt

1

   

   

if

(

cnt

5

)

{

     

break

   

}

}

Running the above code gives the following output:

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

while

A while loop in R repeatedly executes a target statement as long as the given condition is true.

The syntax format is as follows:

while(condition)

{

   statement(s);

}

Here, statement(s) can be a single statement or a block of several statements.

condition can be any expression; it is true for any nonzero value. The loop executes when the condition is true. When the condition is false, the loop exits and program flow continues with the statement immediately following the loop.

The following example outputs the contents of the while block while the variable cnt is less than 7; cnt is a counter variable:

Example

v

<-

c

(

"Google"

,

"Runoob"

)

cnt

<-

2

while

(

cnt

<

7

)

{

print

(

v

)

cnt

=

cnt

1

}

Running the above code gives the following output:

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

for

In the R programming language, a for loop statement can repeatedly execute a specified statement, with the number of repetitions controlled in the for statement.

The syntax format is as follows:

for (value in vector) {

    statements

}

R's for loop is especially flexible; it can iterate not only over integer variables but also over data types such as character vectors, logical vectors, and lists.

The following example outputs the first four of the 26 letters:

Example

v

<-

LETTERS

[

1

:

4

]

for

(

i

in

v

)

{

   

print

(

i

)

}

Running the above code gives the following output:

[1] "A"

[1] "B"

[1] "C"

[1] "D"


Loop control

break

The break statement in R is inserted within a loop body to exit the current loop or statement and begin executing the statement immediately following it in the script.

If you use nested loops, the break statement stops the execution of the innermost loop and begins executing the outer loop statement.

break is also commonly used in switch statements.

The syntax format is as follows:

break

The following example uses break to exit the loop when the variable cnt is 5; cnt is a counter variable:

Example

v

<-

c

(

"Google"

,

"Runoob"

)

cnt

<-

2

repeat

{

   

print

(

v

)

   cnt

<-

cnt

1

   

   

if

(

cnt

5

)

{

     

break

   

}

}

Running the above code gives the following output:

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

[1] "Google" "Runoob"

next

The next statement is used to skip the current iteration and start the next one (similar to continue in other languages).

The syntax format is as follows:

next

The following example outputs the first 6 of the 26 letters, skipping the current iteration and moving to the next when the letter is D:

Example

v

<-

LETTERS

[

1

:

6

]

for

(

i

in

v

)

{

   

   

if

(

i

==

"D"

)

{

 

D is not output; this iteration is skipped and the loop moves to the next

     

next

   

}

   

print

(

i

)

}

Running the above code gives the following output:

[1] "A"

[1] "B"

[1] "C"

[1] "E"

[1] "F"