Updated March 22, 2023

## Introduction to If Statement in R

If statement in R is a control structure that is used to check certain conditions in the programming logic. The if statement works by checking the condition is true or false at the beginning of if block, and in case if it found the condition is true then only it allows R to evaluate the statements which are written in the If scope, Similarly while the conditions found to be false, if statement ignores the R statements inside its scope and moves the program controls out of the if block. The if statement in R generally works with else block for developing the logic.

**Syntax**

The syntax of the if-else statement in R is as follows:

`if (condition) {`

statement_1 # This statement will execute if the condition is satisfied.

}

else {

statement_2 # This section will execute if the condition is not satisfied.

}

The syntax of the if-else if-else statement in R is as follows:

`if (condition_1) {`

statement_block_1 # This block will execute if condition 1 is met.

}

else if (condition_2) {

statement_block_2 # This block will execute if condition 2 is met.

}

else {

statement_block_3 # This block will execute if none of the conditions is met.

}

**Flow Diagram**

Here is the flow diagram mention below

### Examples of If Statement in R

Understanding if statement in R through examples

The if statement in R can be used in various situations, and it works with various types of data. We’ll try to implement it in order to understand its working right from basic to most complex situations. Let’s proceed to understand this in more detail.

#### Example #1

In this example, we’ll check if a number is divisible by 21 or not. We’ll create a function that tests this division condition using the if-else statement. Then, we’ll pass the number to be tested into the function.

The following screenshot shows the function in R that embeds if-else for validating a divisibility test for a number.

`divide.function <- function(x) {`

if (x %% 21 == 0)

{

print(“The number is divisible by 21”)

}

else {

print(“The number is not divisible by 21”)

}

}

In the above code, a number passed into the function will first be validated against a condition in the if statement. So, if the number when divided 21 returns zero as remainder then the code in if block will be printed as the output otherwise else will execute.

Passing different numbers in the above function gives results as shown below:

#### Example #2

In the second example, we will extend the if-else loop to the if-else if-else statement. Here, we intend to check if a number is divisible by both 7 and 9, just by 7 but not by 9, just by 9 but not by 7, or by none of the two numbers. If any condition is satisfied, the corresponding code block will execute. There can be multiple else-if statements. As mentioned earlier, the code block in else executes only when no condition is satisfied. Output statements in corresponding blocks must convey the meaning properly.

`divide.function <- function(x) {`

if (x %% 7 == 0 && x %% 9 == 0)

{

print("The number is divisible by both 7 and 9.")

}

else if (x %% 7 == 0) {

print("The number is divisible by 7 but not by 9.")

}

else if (x %% 9 == 0) {

print("The number is divisible by 9 but not by 7.")

}

else {

print("The number is divisible neither by 7 nor by 9.")

}

}

Passing different numbers in the function defined using the above code generates output as shown in the below screenshot.

Passing different numbers in the user-defined function divide.function() gives the result as above. Here, four types of numbers are possible; firstly those divisible by both 7 and 9 such as 63 and 126, secondly those divisible by 7 but not by 9 such as 14 and 35, thirdly those divisible by 9 but not by 7 such as 18 and 36, and finally those which are divisible by neither of the two integers i.e. 7 and 9. The final condition forms the part of else statement as, all other possible conditions are checked, in the preceding if and else ifs.

#### Example #3

In this example, we will check if a number is a prime number or not. Any number, which is divisible only by itself and one, is called a prime number. However, it must be noted that 1 is not a prime number as it doesn’t satisfy the condition for a number to be prime, because, in the case of 1, both numbers i.e. itself and 1 are the same. Further, what if a user passes a negative number into the function to check whether it is prime or not? So, all the above conditions will have to be taken into consideration while implementing the function. Let’s see how the function looks like when it is implemented, as shown below.

`primecheck.function <- function(x) {`

flag <- 1

if (x <= 0)

{

print("Number must be greater than zero.")

}

else if (x == 1)

{

print("1 is not a prime number.")

}

else

{

for (i in 3:x-1)

{

if (x %% i == 0)

{

flag <- 0

}

}

if (flag == 0)

{

paste(x,"is not a prime number.")

}

else

{

paste(x, "is a prime number.")

}

}

}

- The above screenshot shows the user-defined function primecheck.function() in R. As can be seen, the code uses nested if-else to accomplish the task. Let’s see how the code works. The function primecheck.function() takes only one parameter. Initially, the flag variable is set to 1. Then the if-else if-else section begins. The if statement checks, whether the number is greater than zero or not. This is a very important condition and needs to be the first condition.
- If not, then the appropriate message is generated by the function. Next, the else-if checks if the number is equal to 1 and tells the user that 1 is not a prime number; the reason has been given in the preceding section. If none of the two above conditions are met, then the code block in else section executes. In else, now, we essentially try to check if the number is prime or not through for loop.
- The for loop divides the number from 2 to number minus 1 range by the increment variable, and in this range, if it gets divided then the flag is set to zero, meaning that the number is not a prime number. The if-else statement outside for loop tests a condition on the flag and generates appropriate output.

The function works and gives the results as shown in the below screenshot.

### Conclusion

In R programming, the if-else or if-else if-else statement can be effectively used to work with condition related aspects. The R script may not facilitate the implementation of if statements the same way as other programming languages offer. However, if-else can be beautifully embedded into functions to obtain all possible results.

### Recommended Articles

This is a guide to If Statement in R. Here we discuss the if-else or if-else if-else statement can be effectively used to work with condition related aspects. You may also have a look at the following articles to learn more –