Friday, November 27, 2020

R For Loop

A for loop is the most popular control flow statement. A for loop is used to iterate a vector. It is similar to the while loop. There is only one difference between for and while, i.e., in while loop, the condition is checked before the execution of the body, but in for loop condition is checked after the execution of the body.

There is the following syntax of For loop in C/C++:

  1. for (initialization_Statement; test_Expression; update_Statement)  
  2. {  
  3.     // statements inside the body of the loop   
  4. }  

How For loop works in C/C++?

The for loop in C and C++ is executed in the following way:

  • The initialization statement of for loop is executed only once.
  • After the initialization process, the test expression is evaluated. The for loop is terminated when the test expression is evaluated to false.
  • The statements inside the body of for loop are executed, and expression is updated if the test expression is evaluated to true.
  • The test expression is again evaluated.
  • The process continues until the test expression is false. The loop terminates when the test expression is false.

For loop in R Programming

In R, a for loop is a way to repeat a sequence of instructions under certain conditions. It allows us to automate parts of our code which need repetition. In simple words, a for loop is a repetition control structure. It allows us to efficiently write the loop that needs to execute a certain number of time.

In R, a for loop is defined as :

  1. It starts with the keyword for like C or C++.
  2. Instead of initializing and declaring a loop counter variable, we declare a variable which is of the same type as the base type of the vector, matrix, etc., followed by a colon, which is then followed by the array or matrix name.
  3. In the loop body, use the loop variable rather than using the indexed array element.
  4. There is a following syntax of for loop in R:
  1. for (value in vector) {  
  2.    statements  
  3. }  

Flowchart

R For Loop

Example 1: We iterate all the elements of a vector and print the current value.

  1. # Create fruit vector  
  2. fruit <- c('Apple''Orange',"Guava"'Pinapple''Banana','Grapes')  
  3. # Create the for statement  
  4. for ( i in fruit){   
  5.     print(i)  
  6. }  

Output

R For Loop

Example 2: creates a non-linear function with the help of the polynomial of x between 1 and 5 and store it in a list.

  1. # Creating an empty list  
  2. list <- c()  
  3. # Creating a for statement to populate the list  
  4. for (i in seq(15, by=1)) {  
  5.   list[[i]] <- i*i  
  6. }  
  7. print(list)  

Output

R For Loop

Example 3: For loop over a matrix

  1. # Creating a matrix  
  2. mat <- matrix(data = seq(1021, by=1), nrow = 6, ncol =2)  
  3. # Creating the loop with r and c to iterate over the matrix  
  4. for (r in 1:nrow(mat))     
  5.     for (c in 1:ncol(mat))    
  6.          print(paste("mat[", r, ",",c, "]=", mat[r,c]))   
  7. print(mat)  

Output

R For Loop

Example 4: For loop over a list

  1. # Create a list with three vectors  
  2. fruit <- list(Basket = c('Apple''Orange',"Guava"'Pinapple''Banana','Grapes'),   
  3. Money = c(101215), purchase = TRUE)  
  4. for (p  in fruit)   
  5. {   
  6.     print(p)  
  7. }  

Output

R For Loop

Example 5: count the number of even numbers in a vector.# Create a list with three vectors.

  1. x <- c(2,5,3,9,8,11,6,44,43,47,67,95,33,65,12,45,12)  
  2. count <- 0  
  3. for (val in x) {  
  4. if(val %% 2 == 0)  count = count+1  
  5. }  
  6. print(count)  

Output

R For Loop

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...