For loop in R || For function in R programming language

For loop in R || For function in R programming language


Hey guys, what’s up? Today I am going to discuss you about for loop in r. So let’s start—
I don’t know why some guys are searching like for function in r. For your kind clarification; I want to know you that it is not a function. It is a loop. 

forloop is one of the most popular control flow statement which is used to iterate a vector. To iterate a statements or a part of the program several times, there we need to use for loop. Between forand while loop, there is only one difference. Except this, everything is the same as while loop. 

·         In while loop, condition is checked before execution of the body 
·         In for loop condition is checked after execution of the body.

Syntax of for loop in r

For (i in vector) {
    statement
}

Here, itake on each of its value during the loop. In each iteration, statement is checked/evaluated.

Flowchart of R For Loop

For loop in R || For function in R programming language


Example1: A simple R for Loop

# Create fruit vector
fruit <- c('Apple', 'Orange', 'Passion fruit', 'Banana')
# Create the for statement
for ( i in fruit){ 
 print(i)
}

After executing the program our output looks like below

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

Example 2:  by using the polynomial of x between 1 and 4 creates a non-linear function and store it in a list by using for loop in r programming language

# Create an empty list
list <- c()
# Create a for statement to populate the list
for (i in seq(1, 4, by=1)) {
  list[[i]] <- i*i
}
print(list)

After executing the program our output looks like below

## [1]   1  4 9 16  

Example3: write a program using R for Loop which will check whether a is Prime or not 

# Program to check if the input number is prime or not
# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
flag = 0
# prime numbers are greater than 1
if(num > 1) {
# check for factors
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
}
}
} 
if(num == 2)    flag = 1
if(flag == 1) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}

Output:

Output 1
================================

Enter a number: 25
[1] "25 is not a prime number"

Output 2
================================

Enter a number: 19
[1] "19 is a prime number"

Example4: Find the factorial of a number using for function in r 😀

# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
# check is the number is negative, positive or zero
if(num < 0) {
print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
print("The factorial of 0 is 1")
} else {
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}

After executing the program our output looks like below

Enter a number: 8
[1] "The factorial of 8 is 40320"

There is a built-in function in r programming language. It is factorial().

> factorial(8)
[1] 40320

Example5: Write a Multiplication Table program by using R for Loop

# R Program to find the multiplicationtable (from 1 to 10)
# take input from the user
num = as.integer(readline(prompt = "Enter a number: "))
# use for loop to iterate 10 times
for(i in 1:10) {
print(paste(num,'x', i, '=', num*i))
}

After executing the program our output looks like below

Enter a number: 7
[1] "7 x 1 = 7"
[1] "7 x 2 = 14"
[1] "7 x 3 = 21"
[1] "7 x 4 = 28"
[1] "7 x 5 = 35"
[1] "7 x 6 = 42"
[1] "7 x 7 = 49"
[1] "7 x 8 = 56"
[1] "7 x 9 = 63"


for loop in r over a list

# Create a list with three vectors
fruit <- list(Basket = c('Apple', 'Orange', 'Passion fruit', 'Banana'), 
Money = c(10, 12, 15), purchase = FALSE)
for (p  in fruit) 
{ 
 print(p)
}

Output:

## [1] "Apple" "Orange" "Passion fruit" "Banana"       
## [1] 10 12 15
## [1] FALSE

R for loop over a matrix : A matrix has 2-dimension, rows and columns. We have to define 2 for loop, 1 for the rows and another for the column to iterate over a matrix.

# Create a matrix
mat <- matrix(data = seq(10, 20, by=1), nrow = 6, ncol =2)
# Create the loop with r and c to iterate over the matrix
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]))  

Output:

## [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" 

Today we have seen many example of for loop in r. I hope you guys have understood everything what I have discussed above. On more things, I want to remind you again, it is not for function in r. You guys have to go a long way. So you should learn what it actually called. That’s why, I emphasize it again. So, guys, that’s all for today. Later we will discuss another topic of r programming language. Till then, take care. Happy Coding

Post a Comment