Sunday, November 11, 2018

How to use For loop and Nested For Loop in R Programming

For loop and Nested For loop in R Programming
For loop
The for loop in R is very useful when we need to iterate over a list of elements or a range of numbers. Loop can be used to iterate over a list, data frame, vector, matrix or any other object. The braces and square bracket are compulsory.
Syntax :
>for (value in vector)
 {
   statements
  }


Examples :
#Looping through Vector elements :
>v <- LETTERS[1:4]
>for ( i in v)
{
   print(i)
}
[1] "A"
[1] "B"
[1] "C"

[1] "D"

>for(i in 1:5)
{
print (i^2)
}



[1] 1
[1] 4
[1] 9
[1] 16
[1] 25


>fruits <- c('Apple', 'Orange', 'Passion fruit', 'Banana')
>for ( i in fruits)
{
 print(i)

}
[1] "Apple"
[1] "Orange"
[1] "Passion fruit"
[1] "Banana"

#Looping through List elements :
>mylist <- c()
>for (i in seq(1, 4, by=1))
{
  mylist[[i]] <- i*i
}

print(mylist)
[1]  1  4  9 16

>fruits <- list(Basket = c('Apple', 'Orange', 'Passion fruit', 'Banana'),
Money = c(10, 12, 15), Purchase = FALSE)

>for (p  in fruits)
{
 print(p)
}
## [1] "Apple" "Orange" "Passion fruit" "Banana"      
## [1] 10 12 15
## [1] FALSE

#Looping through Matrix elements :
> mat <- matrix(data = seq(10, 20, by=1), nrow = 6, ncol =2)
Warning message:
In matrix(data = seq(10, 20, by = 1), nrow = 6, ncol = 2) :
  data length [11] is not a sub-multiple or multiple of the number of rows [6]

> mat
     [,1] [,2]
[1,]   10   16
[2,]   11   17
[3,]   12   18
[4,]   13   19
[5,]   14   20
[6,]   15   10

>for (r in 1:nrow(mat))
   {  
    for (c in 1:ncol(mat)) 
      {
         print(paste("Row", r, "and column",c, "have values of", mat[r,c])) 
       }
  }
[1] "Row 1 and column 1 have values of 10"
[1] "Row 1 and column 2 have values of 16"
[1] "Row 2 and column 1 have values of 11"
[1] "Row 2 and column 2 have values of 17"
[1] "Row 3 and column 1 have values of 12"
[1] "Row 3 and column 2 have values of 18"
[1] "Row 4 and column 1 have values of 13"
[1] "Row 4 and column 2 have values of 19"
[1] "Row 5 and column 1 have values of 14"
[1] "Row 5 and column 2 have values of 20"
[1] "Row 6 and column 1 have values of 15"
[1] "Row 6 and column 2 have values of 10"

Nested For Loop :
The placing of one for loop inside the body of another for loop is called nested for loop.  When you nested two loops, the outer loop takes control of the number of complete repetitions of the inner loop. Thus inner loop is executed N- times for every execution of Outer loop.

Example :
>for(i in 1:5)
     {
    for(j in 1:2)
        {
          print(i*j);
         }
    }

[1] 1
[1] 2
[1] 2
[1] 4
[1] 3
[1] 6
[1] 4
[1] 8
[1] 5
[1] 10

For Loop with Break Statement:
A break statement is used inside a loop to stop the iterations and flow the control outside of the loop.
x <- 1:5
>for (i in x)

{
    if (i == 3)

   {
        break
    }
    print(i)

}

[1] 1
[1] 2

For Loop with Next Statement:
The “next” discontinues/skips a particular iteration and jumps to the next cycle. In fact, it jumps to the evaluation of the condition holding the current loop.
>x <- 1:5
>for (i in x)
  {
    if (i == 2){
    next
  }
 print(i)
}

[1] 1
[1] 3
[1] 4
[1] 5


Thanks, TAMATAM

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog