R If Statement

R If statement executes a set of statements based on a given condition. If the condition in If-statement evaluates to TRUE, then the statements in the If block execute, else not.

In this tutorial, we will learn about R If statement and how does this help in decision making with syntax, flow diagram and examples.

Flow Diagram

The flow diagram that shows the flow of execution for an if statement is given below.

ADVERTISEMENT
R If statement - flow diagram

Syntax

The syntax of R If statement is

if (condition) {
     statement1
     statement2  . . .
     statementN
 }

The condition is any expression that evaluates to a boolean value. And if condition = TRUE, execution flow enters the block of If-statement, else it skips the statements’ execution in the block and continues with the statements after If-statement, if any.

Examples

In the following examples, we will go through different scenarios like if the If-condition is TRUE, or If-condition is FALSE; and some use cases where If-statement is used.

If Condition is TRUE

In the following program, we will write an If-statement with condition that checks if the value in variable a is 6.

Example.R

a <- 6

if (a == 6) {
  print ("Value in a is 6.")
}

print("End of program.")

Output

[1] "Value in a is 6."
[1] "End of program."

Since the condition a == 6 evaluates to TRUE, the code inside If block is executed.

If Condition is FALSE

Now, let us take a value for a, say a = 10.

Example.R

a <- 10

if (a == 6) {
  print ("Value in a is 6.")
}

print("End of program.")

Output

[1] "End of program."

Since a is 10, the condition a == 6 evaluates to FALSE. Therefore, the code inside If-block is not executed.

Check if Number is Even using If

In this example, we use If-statement to check if a number is even. If n is the number, then If-condition could be n%%2 == 0.

Example.R

n <- 10

if (n %% 2 == 0) {
  print ("Number is even.")
}

Output

[1] "Number is even."

Check if Strings are Equal using If

In this example, we use If-statement to check if two strings are equal. If str1 and str2 are the strings, then the condition to check if these two strings are equal is str1 == str2.

Example.R

str1 <- "hello"
str2 <- "hello"

if (str1 == str2) {
  print ("The two strings are equal.")
}

Output

[1] "The two strings are equal."

Using AND Operator in IF Condition

In this example, we check if given number is even and divisible by 3. Since the number has to satisfy both the conditions, we may join the condition using AND operator.

Example.R

n <- 12

if ((n %% 2 == 0) && (n %% 3 == 0)) {
  print ("Number is even and divisible by 3.")
}

Output

[1] "Number is even and divisible by 3."

Using NOT Operator in IF Condition

In this example, we check if given number is not even number. Since we have to check the negation of the condition that the number is even, we use NOT operator for the condition.

Example.R

n <- 11

if (!(n %% 2 == 0)) {
  print ("Number is not even.")
}

Output

[1] "Number is not even."

Conclusion

In this R Tutorial, we learned about R If-statement, its syntax and how this if statement helps in decision making, with examples.