Learn R Online
 Home            


R if...else Statement

Learn how to write if statement and if...else statement in R programming with Examples and Flowchart.

Conditional statements form the building blocks of any programming language for decision making. In R programming a conditional statement consists of atleast one test condition which is a boolean expression that evaluates in to either True or False which forms the basis of execution or non execution of certain code. This is done in R using if & if...else statement. 

R if statement

if statement is used when there is only one test condition and on the basis of that you have to take a decision. First the condition is checked. If the boolean expression results in to True the body of if statement is executed, otherwise R code in the specific block is skipped. 

The syntax of if statement is

if (test condition) {

    body of if

}

Now in case test condition is True, body of if is executed. When test condition is false the code in the body is ignored.


Flowchart of if statement

You can see the idea here in flowchart.

Flowchart of if in R programming


Example: if statement

score <- 70

if (score > 50){

    print('PASS')

}

When you write the code in rstudio it will be like 

if statement example in R Programming

and the output on rstudio is 

R if statement output in Rstudio

As the score is 75, hence the condition will result in a True and it will execute the code in the body of if statement i.e. it will print "PASS" on console.

if...else statement

if you want to execute certain code in case the test condition results in to a False you use if else statement.

The basic syntax for creating an if...else statement is 

if (condition) {

    //code to be executed if condition is True

} else {

    //code to be executed if condition is False

}

Flowchart of if else statement:

Flowchart of if...else statement in R programming

Example of if...else statement:

Here we are adding else code in the previous example. 

score <- 45

if ( score > 50){

print("PASS")

}  else  {

print("FAIL")

}

if else statement example output in Rstudio

R if else statement output in Rstudio

This code can also be written in a single line 

>if (score > 50)  print('PASS')  else  print('FAIL')

if else in R programming allows us to use this statement in this way also

>age <- 19

>status <- if(age > 18) "Adult" else "Child"

>status

>[1] "Adult"

First we are assigning 19 to a variable named age

Then we are creating a new variable status, however the value assigned to it depends on a condition. If the age variable is more than 18 then "Adult" will be assigned to status, otherwise "Child" will be assigned to it. In our example the variable age is 19, hence status will be assigned "Adult" string, which is shown in output. This construct makes the code neat and more readable.

This method can also be used with if statement also

>score <- 55

>Grade <- if(score > 50) "PASS"

>Grade

>[1] "PASS"

The if...else if...else statement

The if statement can also be followed by an else if...else statement. This is used when you want to check several conditions using if...else structure. This is also called as if...else ladder used for more than two alternatives. 

The syntax of if...else if...else is 

if (condition no 1) {

    //code to be executed if condition no 1 is True

} else if(condition no 2) {

    //code to be executed if condition no 2 is True

} else if(condition no 3) {

    //code to be executed if condition no 3 is True

} else { // code to be executed if all conditions are False 

}

Here only one block of code will be executed depending on the condition. 

Example of nested if statement:

Here is the Grade Key:

For assigning a grade based on percentage of marks

90% or more  "A Grade"

80% or more  "B Grade"
70% or more  "C Grade"
60% or more  "D Grade"
less than 60%  "F Grade"

You have to write an R program which outputs grade when score is given to it. Lets see how it will be done and also the output using rstudio

if...else-if...else statement in R Programming

if...else if...else output Rstudio

You can see the output is "C" which is correct. You may write this program, change score to 95, 82,60 and 45 and see what is the output of the code. There are 5 possible output and you should check to see if the program is working fine. This is testing your code and debugging it for any logical errors.

After this tutorial you should be able to understand decision making in R programming which consists of if statement, if...else statement and nested if statement. These statements form the core of R programming practise variety of code exercises to have grasp over these constructs. 


Learn to Shine

  • Programming made Easy for all
  • High School and College
  • University level
  • Professionals doctors, engineers, scientists
  • Data Analysts