Browse By

R Language –R Loop: Online Course

R Language –R Loop: Online Course: So many programming language are available but R language is used for specific Statistical Computing and Data Analysis. Loops are used in programming to repeat a specific block of code. There are three main types of loop in R: the for loop, the while loop and the repeat loop.

What is loop?

“Looping”, “cycling”, “iterating” or just set of operations to be repeated several times, It is know as loop. Loops can execute a block of code as long as a specified number of times or until a specified condition is met. Loops are easily handable because they save time, reduce errors, and code more readable.

Loops in R (for, while, repeat)

In R programming, we require a control structure to run a sequence of code multiple times. To execute the same lines of code multiple times in a program, a programmer can simply use a loop. There are three main types of loop in R: the for loop, the while loop and the repeat loop.

R For Loop

In R, the for loop is used to repeat the elements of a vector or sequence. It is one of the most used loops in any programming language. Let’s look at the syntax of a for loop in R:

Syntax for for Loop in R

for(val in vector/sequence){
some_statement(s)
}

Example 1: Let’s say we want to run a for-loop that iterates over a vector with ten elements (ie 1:10). In each iteration, we want to add +1 to our data object and we want to print this data object. For this we can use the following R code:

Input: Write a program to print 1 to 10 value
for (x in 1:10) {
print(x)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10

Example 2

Input:
birds <- list(“Crow”, “Peacock”, “Dove”, “Sparrow”)
for (x in birds) {
  print(x)
}
Output:
[1] “Crow”
[1] “Peacock”
[1] “Dove”
[1] “Sparrow”

Example 3: Let’s say we want to run a for-loop that iterates over a vector with ten numbers (ie 1:10). In each iteration, we want to number is multiply by 9 and add +1 to our data object and we want to print this data object. For this we can use the following R code:

Input: Write a program to print table
numbers <- c(1:10)
for (num in numbers) {
print(9 * num)
}
Output:
[1] 9
[1] 18
[1] 27
[1] 36
[1] 45
[1] 54
[1] 63
[1] 72
[1] 81
[1] 90

If .. Else Combined with a For Loop

Input: Print “info khajana” If the dice number is 6:
dice <- 1:6

for(x in dice) {
  if (x == 6) {
    print(paste(“The dice number is”, x, “info khajana”))
  } else {
    print(paste(“The dice number is”, x, “info khajana”))
  }
}
Output:
[1] “The dice number is 1 Not info khajana”
[1] “The dice number is 2 Not info khajana”
[1] “The dice number is 3 Not info khajana”
[1] “The dice number is 4 Not info khajana”
[1] “The dice number is 5 Not info khajana”
[1] “The dice number is 6 info khajana”

In this program the value from 1 to 5 is checked and in that condition “Not info khajana” is printed, as soon as it gets 6 value it prints “info khajana”.

Nested Loops

R – while Loop

In the while loop, until the given condition is not resolved, then the statement given in it keeps on repeating.

In the while loop, as long as the condition is true, the given statement is repeated and when the condition becomes false then the repetition of the statement stops.

Syntax for while Loop in R

while(condition){
some_statement(s)
}

Example 1: In the code, the i variable is initialized to 1. And the condition is given to the loop until the i variable is not less than 6, then the loop continues to execute.

Input: To print a value less than 6
i <- 1
while (i < 6) {
print(i)
i = i+1
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

Note: – If the condition becomes false in the beginning itself. So the compiler does not go inside the loop. i.e, skips the loop.

if-else statement within a while loop

In the following program, even and odd numbers are printed in different ways-

Input: write a program that will print 1 to 25, excluding the multiples of three
i <- 1
while (i<= 25) {
i = i + 1
if (i %% 3 == 0)
print(i)
}
Output:
[1] 3
[1] 6
[1] 9
[1] 12
[1] 15
[1] 18
[1] 21
[1] 24

R – repeat Loop

In the repeat loop, when the given condition in the repeat loop is not resolved, the given statement is repeated again and again.

Syntax for repeat Loop in R
repeat{
some_statement(s)
if(condition) {
break
}
}

Example for repeat Loop in R

In the example, the statement will be repeated until the loop gets i greater than 10. When i > 10 is found then the loop will be broken or stopped with the break keyword.

Input: R program to illustrate repeat loop
i = 0
repeat{
cat(i, “\n”)
i = i + 1
if(i > 10){
break
}
}
Output:
0
1
2
3
4
5
6
7
8
9
10

CONCLUSION

We have discussed in this article such as R loop such as(for loop, while loop & repeat loop) . A good understanding of any other programming languages ​​will help you understand R programming concepts quickly.

Read Next Article in the Series-

Break

आशा करता हूँ, कि यह आर्टिकल आपको पसंद आया होगा तो सोच क्या रहे हैं अभी इसी वक्त इसे अपने दोस्तों के साथ सोशल मीडिया पर Share करें।

ThankingYou………………धन्यवाद………………..शुक्रिया………………..मेहरबानी…………………..

Reference

R Language

Read More

1. Introduction of R
2. Install R