Decision Making in R (Chapter 1 Episode 5)

Decision making is very important in any programming language. Decision making involves creating multiple conditions that are to be tested by the program, together with statements that will be executed when a condition is true, and optionally, the statements that are to be executed when the condition is false.

if statement

The if statement consists of a Boolean followed by one or more statements. The statement takes the syntax given below:

if(boolean_expression) { 
// statement(s) to be executed if the Boolean expression evaluates to true. }

If the specified condition is found to be true, the statements inside the curly brace will be executed. If the specified condition evaluates to a “false”, the statements immediately after the if statement will be executed. Consider the example given below:

y <- 12L 
if(is.integer(y)) { 
print("Y is an Integer") 
}

output

[1] "Y is an Integer"
age <- 16 
if(age < 18){ 
print("Your age is less than 18 years") 
}

output

[1] "Your age is less than 18 years"

In the if expression, we are checking whether the value of variable age is less than 18. We had set the value of age to 16. Since the condition evaluates to a true, the statement below it was executed. The bad thing about the if expression is that nothing is done when the condition is false. Let us change the value of age to 20:

age <- 20 
if(age < 18){ 
print("Your age is less than 18 years") 
}

Since the value of age is greater than 18, the condition will evaluate to a false. The code will return nothing. We need a way of specifying what should be done when the condition is false. This is what we will discuss next.

if...else statement

In the if…else statement, the if helps us set the condition and the statements to be executed when the condition is true. The else part helps us specify the statements that will be executed when the condition is false. This statement takes the following syntax:

if(boolean_expression) { 
// statement(s) to execute if the Boolean expression evaluates to true. 
} else { 
// statement(s) to execute if the Boolean expression evaluates to false. 
}

If the condition evaluates to a true, the statements within the if block will be executed, while if the condition evaluates to a false, the statements within the else block will be executed. Let us use the example of checking the value of age:

age <- 20 
if(age < 18){
 print("Your age is less than 18 years") 
} else { 
print("Your age is above 18 years") }

output

[1] "Your age is above 18 years"
y <- c("what","was","it")
if("it" %in% y) { 
print("it was found")
} else { 
print("it was not found") }

output

[1] "it was found"

else if statement

else if statement is used when we have more than 2 conditions to be executed. It starts with an if statement, followed by an optional else…if…else statement, making it good for testing a number of conditions. The ladder takes the syntax given below:

if(boolean_expression 1) { 
// to execute if the Boolean expression 1 is true. 
} else if( boolean_expression 2) { 
// to execute if the boolean expression 2 is true.
 } else if( boolean_expression 3) { 
// to execute if the boolean expression 3 is true. } else {
 // to execute if none of the above condition is true. }
y <- c("what","was","it") 
if("It" %in% y) { 
print("it was found on the first time") 
} else if ("it" %in% y) { 
print("it was found on the second time") 
} else { 
print("No it was found") }
[1] "it was found on the second time"

The vector y has three strings, what, was, and it. The search is case sensitive. In the if condition, we are searching for It, not it. The condition will become false.

y <- 0 
if (y < 0) { 
print("y is a Negative number") 
} else if (y > 0) { 
print("y is a Positive number") 
} else print("y is Zero")

output

[1] "y is Zero"
a <- 67
b <- 76
c <- 99


if(a > b && b > c)
{
    print("condition a > b > c is TRUE")
} else if(a < b && b > c)
{
    print("condition a < b > c is TRUE")
} else if(a < b && b < c)
{
    print("condition a < b < c  is TRUE")
}

output

[1] "condition a < b < c  is TRUE"

Switch Statement

The switch statement allows for a particular variable to be tested for equality against a list of values. Each value is referred to as a case, and the variable to be switched has to be checked for every case that exists. For example:

switch(expression, case1, case2, case3....)
  • R supports a number of decision-making statements. These help us evaluate a condition or a set of conditions and take action based on the outcome of the evaluation.
  • The if statement checks for the truth value of a condition and instruct the R interpreter on what to do when the condition is true.
  • The if…else statement uses an if statement and an else part to specify the action to perform when the condition is false.
  • The if…else ladder helps us evaluate multiple conditions and take different actions based on the outcome of each condition.
  • The switch statement helps us check or evaluate the value of an item against a set of values.
y <- switch("age", "name" = "John", "age" = 20, "country" = "USA")

output

> y
[1] 20