For Loop in R

For is a keyword reserved for a loop statement. The for loop executes a block of statements continuously as long as the condition of the loop is true. The loop stops only when the condition is false. Thus, the block of codes executes only a specified number of times over a sequence (dictionary, string, list, set or tuple).

Syntax:

for (value in vector) {
   statements
}

Example 1:

var <- LETTERS[1:10]
for ( i in var) {
   print(i)
}

Output:

[1] "A"
 
[1] "B"
 
[1] "C"
 
[1] "D"
 
[1] "E"
 
[1] "F"
 
[1] "G"
 
[1] "H"
 
[1] "I"
 
[1] "J"

Example 2:

even <- c(2,4,6,8)
 
for (i in even) {
  print(i)
}

Output:

[1] 2
 
[1] 4
 
[1] 6
 
[1] 8

Example 3:

colors <- list("red", "yellow", "blue", "white", "pink", "green")
 
for (x in colors) {
  print(x)
}

Output:

[1] "red"
 
[1] "yellow"
 
[1] "blue"
 
[1] "white"
 
[1] "pink"
 
[1] "green"

Example 4:

dice <- 1:6
 
for (x in dice) {
  if (x == 6) {
    cat("\n\nRolling Dice =", x, "\nWINNER!!")
  } else {
    cat("\nRolling Dice =", x, "\nTry Again!")
  }
}

Output:

Rolling Dice = 1
 
Try Again!
 
Rolling Dice = 2
 
Try Again!
 
Rolling Dice = 3
 
Try Again!
 
Rolling Dice = 4
 
Try Again!
 
Rolling Dice = 5
 
Try Again!
 
Rolling Dice = 6
 
WINNER!!

Example 5:

var <- 1:10
for (i in var) {
if (i == 3){
next
}
print(i)
}

Output:

[1] 1
 
[1] 2
 
[1] 4
 
[1] 5
 
[1] 6
 
[1] 7
 
[1] 8
 
[1] 9
 
[1] 10

Example 6:

var <- c(11,22,33,44,55,66,77,88,99,-100)
sum <- 0
for(n in var){
    if(n<0){
        next
    }
    sum = sum + n
}
cat("Sum of all +ve numbers in var =",sum)

Output:

Sum of all +ve numbers in var = 495

Example 7:

var <- LETTERS[1:9]
for ( i in var) {
if (i == "F") {
        next
}
    print(i)
}

Output:

[1] "A"
 
[1] "B"
 
[1] "C"
 
[1] "D"
 
[1] "E"
 
[1] "G"
 
[1] "H"
 
[1] "I"

Example 8:

bag <- list("bottle", "lunch", "books", "pen")
 
for (item in bag) {
  if (item == "lunch") {
    next
  }
  print(item)
}

Output:

[1] "bottle"
 
[1] "books"
 
[1] "pen"

Example 9:

bag <- list("bottle", "books", "lunch", "pen")
 
for (x in bag) {
  if (x == "lunch") {
    break
  }
  print(x)
}

Output:

[1] "bottle"
 
[1] "books"

Example 10:

colors <- list("red", "yellow", "pink", "white")
 
items <- list("flower", "rose", "cap")
  for (x in colors) {
    for (y in items) {
    if (y == "cap") {
    break
  }
      print(paste(x, y))
  }
}

Output:

[1] "red flower"
 
[1] "red rose"
 
[1] "yellow flower"
 
[1] "yellow rose"
 
[1] "pink flower"
 
[1] "pink rose"
 
[1] "white flower"
 
[1] "white rose"

Example 11:

for (odd in c(1,3,5,9,11)) {
  for (i in c(1,3)) {
      if (odd==11)
        break
     print(odd)
  }
}

Output:

[1] 1
 
[1] 1
 
[1] 3
 
[1] 3
 
[1] 5
 
[1] 5
 
[1] 9
 
[1] 9

Example 12:

N = 23
temp = 0
if(N > 1) {
    temp = 1
    for(i in 2:(N-1)) {
        if ((N %% i) == 0) {
            temp = 0
            break
        }
    }
}
if(N == 2) temp = 1
if(temp == 1) {
    print(paste(N,"is a Prime Number."))
} else {
    print(paste(N,"is not a Prime Number."))
}

Output:

[1] "23 is a Prime Number."
Please follow and like us:
Content Protection by DMCA.com