Table of contents
1.
Introduction
2.
Decision making in R
3.
Types of Decision Making Statements in R 
3.1.
if statement: 
3.1.1.
Syntax 
3.1.2.
Code Example 
3.2.
if-else statement: 
3.2.1.
Syntax 
3.2.2.
Code Example 
3.3.
if-else if-else statement: 
3.3.1.
Syntax 
3.3.2.
Code Example 
3.4.
switch statement:
3.4.1.
Syntax
3.4.2.
Code Example
4.
Using Logical Operators in Decision Making in R
4.1.
Logical AND (&&):
4.2.
Logical OR (||):
5.
Frequently Asked Questions
5.1.
What is R programming language?
5.2.
What are the advantages of the R programming language?
5.3.
Is R language in demand today?
5.4.
What is the purpose of the ifelse() function in R, and how does it differ from if-else statements?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

What is Decision Making in R?

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In R or any other programming language, decision making statements play a vital role in deciding whether to execute a block of code or not, based on the condition specified in it. 

There are different types of decision making terms which you can use based on your requirement. Wondering how? Not to worry, Ninja! 

This article covers the different types of decision making statements in R along with examples which contain syntax and implementation in detail. 

decision making in R

So, without any delay, let us get started with Decision making in R. 

Decision making in R

In simple terms, Decision-making statements are instructions or rules that help a computer program make decisions based on certain conditions. These statements are like giving the computer specific instructions to follow based on different situations. They are also known as Conditional based statements

Let's imagine you have a computer program that helps the boy decide what to eat based on the day. 

Example: 

Decision making in R

The boy is allowed to eat: 

=> Pizza on Monday,

=> Sandwich on Tuesday, 

=> Brownie on Wednesday,

=> Ice Cream on Thursday! 

 

Hence, based on the day, we will assign the food to the boy. 

To accomplish this we have various types of decision making statements in R. Let us discuss them: 

Types of Decision Making Statements in R 

In R, there are several types of decision-making statements that allow you to control the flow of your program based on different conditions. The commonly used decision-making statements in R are:

if statement: 

The if statement allows you to execute a block of code if a specified condition is true. If the condition is false, the code block is skipped. 

if statement

The basic syntax of the if statement is as follows:

Syntax 

if (condition) {
  # Code to be executed if condition is true
}

Code Example 

message <- TRUE
if (message) {
  print("If statement got executed")
}


Output

[1] "If statement got executed"


Let us modify this code by making the message variable = FALSE to see the result. 


Modified Code 

message <- FALSE
if (message) {
  print("If statement got executed")
}


Output

No Output 


From the output itself it is very clear that to execute a block of code the if statement has to be true. By using this powerful concept, we can make our code more efficient. 

The next statement is if-else statement: 

if-else statement: 

The if-else statement extends the functionality of the if statement by allowing you to execute one block of code if the condition is true and another block of code if the condition is false

if-else statement

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

Syntax 

if (condition) {
  # Code to be executed if condition is true
} else {
  # Code to be executed if condition is false
}

Code Example 

message <- FALSE

if (message) {
  print("If statement got executed")
} else{
  print("else statement got executed")
}


Output 

[1] "else statement got executed"


In the example above, the message contains the FALSE value, hence it falls under the else part. 

Next-up let us see if we can add multiple conditions in the program or not. 

if-else if-else statement: 

The if-else if-else statement, also known as the "else if ladder," is used when you have multiple conditions to check. It allows you to test multiple conditions one by one and execute the corresponding code block of the first condition that evaluates to true

if-else if-else statement

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

Syntax 

if (condition1) {
  # Code to be executed if condition1 is true
} else if (condition2) {
  # Code to be executed if condition2 is true
} else {
  # Code to be executed if all conditions are false
}

Code Example 

num <- 12
if (num < 0) {
  print("number is negative")
} else if (num == 0) {
  print("number is zero")
} else {
  print("number is positive")
}


Output 

[1] "number is positive"


You can try passing different input to see the working of if-else if-else statement. 

switch statement:

The switch statement allows you to select one of several code blocks to execute based on the value of a specific expression. It is useful when you have a single expression with multiple possible values and you want to execute different code blocks based on those values. 

switch statement

The basic syntax of the switch statement is as follows:

Syntax

switch(expression,
       value1 = {
         # Code to be executed if expression matches value1
       },
       value2 = {
         # Code to be executed if expression matches value2
       },
       default = {
         # Code to be executed if expression doesn't match any value
       }
)

Code Example

day <- "wednesday"

result <- switch(day,
  "monday" = {
    # Code for case "monday"
    "Result for Monday"
  },
  "tuesday" = {
    # Code for case "tuesday"
    "Result for Tuesday"
  },
  "wednesday" = {
    # Code for case "wednesday"
    "Result for Wednesday"
  },
  "thursday" = {
    # Code for case "thursday"
    "Result for Thursday"
  },
  # Default case
  {
    # Code for default case
    "Result for default case"
  }
)

print(result)


Output 

[1] "Result for Wednesday"


These decision-making statements in R provide you with the flexibility to control the program flow based on different conditions and make your code more dynamic and responsive.\

Using Logical Operators in Decision Making in R

In R, logical operators are commonly used in decision-making statements to evaluate multiple conditions. The two primary logical operators are && (logical AND) and || (logical OR). 

Logical AND (&&):

The && operator is used to check if multiple conditions are all TRUE.

It returns TRUE only if all the conditions are TRUE; otherwise, it returns FALSE.

Example:

x <- 5
y <- 10

if (x > 0 && y > 0) {
  print("Both x and y are greater than 0.")
}


Output

[1] "Both x and y are greater than 0."

Logical OR (||):

The || operator is used to check if at least one of the conditions is TRUE.

It returns TRUE if any of the conditions is TRUE; otherwise, it returns FALSE.

Example:

x <- 5
y <- 10
if (x > 0 || y > 0) {
  print("Either x or y (or both) is greater than 0.")
}


Output

[1] "Either x or y (or both) is greater than 0."

Frequently Asked Questions

What is R programming language?

The R programming language allows you to analyze data, machine learning, data science models, etc. It helps you create and modify packages, functions, etc. It is an open-source programming language. It is in high demand nowadays.

What are the advantages of the R programming language?

It is an open-source programming language compatible with other languages. It is independent of the platform it is being used on. It offers several packages for data analysis. It is also used for machine learning algorithms, data science, etc.

Is R language in demand today?

R programming language is trendy among data scientists and analysts because it helps them import and clean their data to perform analysis. Also, many companies use the R language.

What is the purpose of the ifelse() function in R, and how does it differ from if-else statements?

The ifelse() function in R is designed to provide a vectorized approach to conditional statements. It allows you to apply a condition to each element of a vector or a set of vectors and return a corresponding value based on the outcome of the condition.

Conclusion

In this article, we studied decision making statements in R and its types, along with some significant examples using R language. We have also discussed some frequently asked questions based on the same. 

If you want to dive deep into the knowledge pool of R programming language, do read the following:-


To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available. Please check out our interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass