R If Else Conditions
Overview
If else statement in R can be categorized as a conditional statement or a decision-making statement.
The if-else statement allows us to make conclusions and execute code blocks based on defined conditions. Moreover, it can also be used for various purposes like data validation, error handling, controlling program flow, and filtering datasets based on specific requirements.
This versatility makes the if-else statement in R a powerful tool for implementing complex logic and handling different scenarios.
Introduction to R if-else Statement
When programming in R, the if else statement helps implement logic and makes complex decision-making easier. Moreover, it can handle not only two but multiple scenarios to evaluate different situations sequentially.
To make if else statements more powerful, we can combine them with logical operators like "and", "or", and "equals" to make more situations to deal with.
For example, an if else statement in R can be constructed as follows - If there is homework, a student will study, or else the student can play.
Now let us add logical operators in this situation.
If the homework is finished by more than 80% then the student can play or else if the homework is completed by more than 50% then he can finish the work and play, else the student has to complete all the homework first.
Syntax
To understand the syntax, let us consider a simple example for assigning grades.
If the mark of a student is greater than 90, the grade to be assigned is A, for less than 90 but greater than 70 we have B, and everything else is C.
The code for this using the if else statement in R will be as follows:
Output:
Here, with if else in R, we have also used logical operators.
First off, we have assigned the value for marks as 85. Then, we have our if else statements in R to decide which category the variable fit in. In the if statement, the condition is for the marks to be greater than 90. Since the condition is not met, we move on to the next line. Here, the condition is for the marks to be greater than 70. This matches our condition, thus the grade variable receives the value of B. Now that we already have an assigned value, we don't check for the last condition and skip the else statement.
R if-else Statement Flowchart
Now that we have an idea of how if else in R works, let us look at a visual representation to understand the control flow.
- To begin with, we have a condition to be taken care of in R programming. This condition is written in the diamond.
- The condition can be anything ranging from coin flip result, dice value result, grades assignment, and many more. The value of this can be either true or false.
- Whether it be true or false, we are taken to a rectangular box which is an action box. This is where we determine what is to be done.
- For example, if the coin is a head, it is assigned as true, else false.
- After executing it, we have the final result or there can be another decision-making statement as well.
Working of if-else Statements in R Programming
- The condition is evaluated:
The if-else statement starts by evaluating a condition enclosed within parentheses. The condition can be a logical expression, a comparison, or any expression that returns a logical value (TRUE or FALSE). - Execution of code blocks:
If the condition is TRUE, the code block immediately following the if statement is executed. This code block can consist of one or more R expressions enclosed within curly braces {}. If the condition is FALSE, the code block following the else statement (if present) is executed. - Single-line expressions:
If the code block within an if or else statement contains only a single expression, you can omit the curly braces {}. However, it is generally recommended to use curly braces even for single-line expressions to enhance code readability and avoid potential issues. - Optional else statement:
The else statement is optional in an if-else statement. If the condition in the if statement is FALSE and there is no else statement, the program simply continues with the next statement after the if-else block.
R if-else Statement Example
Here are some examples highlighting the significance of if-else statements in R:
- Conditional Execution:
If-else statements enable the execution of different code blocks based on specific conditions. This allows programs to adapt and perform different actions depending on the situation. For example:Output:
- Handling Multiple Conditions:
If else in R can be used to handle multiple conditions and execute the relevant code blocks accordingly. This allows for more complex decision-making and handling of different scenarios. For example:Output:
Nested if-else Statement in R
In R programming, you can use nested if-else statements to create more complex conditional logic. Nested if-else statements allow you to specify multiple levels of conditions and corresponding actions.
Let us now consider a complicated example of having nested if-else statements in R.
Here, the function vacation_plans evaluates the destination, duration, and budget inputs to suggest suitable vacation plans.
If the destination is "beach" or "mountains," it considers the duration and budget constraints. For longer durations and higher budgets, it suggests luxurious or comfortable vacations, respectively. For shorter durations, it recommends a short vacation if the budget allows. If the duration is too short, it indicates that it's not feasible for a vacation. If the destination is not recognized, it informs the user about the lack of information.
The function provides tailored recommendations or limitations based on the given inputs, allowing users to make informed vacation decisions.
R if-else Ladder
An if-else ladder is a construct that allows you to handle multiple conditions hierarchically. It consists of a series of if-else statements that are connected, with each if or else if statement evaluating a specific condition.
The conditions are checked sequentially, and the corresponding code block associated with the first true condition is executed. If none of the conditions is true, the code block within the final else statement (if present) is executed.
Here's a step-by-step explanation of how an "if-else ladder" works:
- The first if statement is evaluated. If its condition is true, the code block within that if statement is executed, and the program exits the entire "if-else ladder" construct.
- If the condition of the first if statement is false, the program moves to the next else if statement (if present) and evaluates its condition. If the condition is true, the code block within that else if statement is executed, and the program exits the entire "if-else ladder" construct.
- This process continues for each subsequent else if statement, with the conditions evaluated one by one in sequential order. If a condition is true, the corresponding code block is executed, and the program exits the entire "if-else ladder" construct.
- If none of the conditions in the if and else if statements are true, the code block within the final else statement (if present) is executed. The else statement serves as a catch-all for all remaining cases.
For example:
Output:
Conclusion
- Decision-making:
If-else statements in R allow for decision-making based on specific conditions, enabling programs to adapt and execute different code blocks accordingly. - Complex logic implementation:
If-else statements facilitate the implementation of complex logic by providing the ability to handle multiple conditions in a hierarchical manner. - Nested if-else statements:
R supports nested if-else statements, allowing for the creation of more intricate conditional logic and the handling of multiple levels of conditions.
Overall, if-else statements in R are powerful tools for implementing conditional logic, making decisions, and handling different scenarios effectively in programming tasks.