Controlling the flow of any programming language is a tricky but beautiful task. I will go farther and say every programming language needs the control structures either for decision making or for reducing the repetitive lines of code by looping. In R programming, we have some control structures which either help us in decision making (for example, if-else) or help us in looping(for loop, while loop, etc). In this article and the next one, we will study the control structures in R.
Having a hold on control structure makes a good programmer and that’s the reason we are going to study below-listed control structures in R thoroughly.
if - else statement
ifelse() function
switch statement
for loop
while loop
break statement
next statement
report loops
Let’s first discuss the if-else statement in R programming
The if-else statement is the most commonly used control structure for decision making under R. This statement allows the user to make a decision based on the output which will always be, whether the condition is satisfied(i.e. TRUE) or not satisfied(i.e. FALSE). The working rule for if-else statement is as follows:
The if part of the statement uses a logical condition and checks whether it is true or not. It also includes a block of code that needs to be executed if the condition is true.
Whereas, else part of the statement is something which holds a block of code that gets executed when the condition mentioned under if the part is FALSE.
Syntax of if-else statement is as shown below in R:
Let us see the example code for an if-else statement.
Code Illustration 1
In this code, we have created a variable “x” with value 10. We are now using an if-else condition in a way that, if the value under “x” is greater than zero then the system should print out “x is a positive number” else, the system should print out “x is a negative number”.
Let us see the output this code generates.
Code Output 1
Since the value stored under variable x is greater than zero, the if part of the condition is TRUE. Thus, we are getting output as “x is a positive number” printed on the screen.
The ifelse() function in R works in the same way if-else statements do. The reason for this function to be introduced in the R programming language is associated with the speed of execution. Under R, most of the time a vector/s is used as an argument/s. It becomes a speedy execution of a block of code when we use a vector as an argument under a function rather than the if-else block of statements. This function is introduced for speedy execution of if-else conditional statements. As mentioned earlier, there is no difference in execution.
The syntax for the ifelse() function under R programming language is as shown below:
ifelse(test, yes, no)
Where,
test - is a required argument that specifies the logical condition to be tested and returns a boolean output (either True or False).
yes - is a required argument that specifies the expression which gets evaluated if the logical condition is True.
no - is a required argument that specifies the expression which gets evaluated if the logical condition is False.
See an example that can give us a better overview of the ifelse() function in a better manner.
Code Illustration 2
In this code snippet, since the value under the variable created is negative, we should get an output as “x is a negative number” printed out after the execution of the code.
Code Output 2
You could see that the execution of the if-else statement and ifelse() function is all the same. The only difference will be with the speed with which both of them execute the code and return the output. ifelse() function will surpass the if-else statement in execution and hence is popular among the user if you need to test a single condition. When there are multiple conditions to be checked, if-else statement is the only alternative.
Let us move towards the next of the control statements, switch.
When you have to choose between multiple alternatives from if-else (multiple if-else more precisely) statements, it becomes hard to choose the right one among those. This affects the speed of execution of the system. However, we still have an option for that in R. We can use the switch statement in R which allows us to select or choose between multiple possible alternatives.
When we pass an argument in switch statements under R, we get an output among all the alternatives based on the value of that argument. By the way, it consists only of a single argument. Therefore, no hectic task of defining multiple arguments as like multiple if-else statements.
Syntax for switch statement is as shown below:
switch(expression, list)
Where,
expression - is a condition that gets evaluated under the statement.
list - are the executable pieces of codes out of which a one that satisfies the condition will get printed out.
Let’s see an example of a switch statement in R programming.
Code Illustration 3
Here in this code, we have a series of statements out of which, one should get evaluated based on the expression value. Since the expression value is 2, ideally the second line of code should get evaluated. Let’s run this code and see the output.
Code Output 3
And yes, it works perfectly the way we thought. It returns the statement “Football is my favorite game” as an output since that is the second line of the list under the switch.
The most widely used loop under any programming language is for a loop. Whenever you have a task where a piece of code should repeat for the given set of values, you use for a loop. This is as simple as it is under R as well. You specify a set of values, and the code will repeat for each value in that set.
Syntax for “for loop” is as shown below:
Let us see an example associated with for loop in R.
Code Illustration 4
In this example, we have defined a set of values 1 to 10 stored under a variable named “val”. We are using a for loop to square up each value under the variable as shown in the screenshot below. The “i” used in for loop is nothing but a placeholder which specifies the position of each element under variable “val”
Let’s see if this code actually works and give us the square values for 1 to 10 each.
Code Output 4
We can see that the code is working perfectly fine and we are getting the square values for each element under vector “val”.
In this article, we have learned about the first four control structures. If-else statement and ifelse() function are used to get a conditional output in R programming. Both work in the same manner; however, ifelse() function has better execution speed than the if-else statement.
Switch returns an output out of multiple alternatives based on the expression value provided as an argument. Finally for loop is used to reduce the repetitive task. It allows you to write a small piece of code that repeats the task for a series of values provided.
We will stop here and come up with our next article which will cover the remaining four parts of the control structure under R programming. Until then, stay safe! 😊
5 Factors Influencing Consumer Behavior
READ MOREElasticity of Demand and its Types
READ MOREAn Overview of Descriptive Analysis
READ MOREWhat is PESTLE Analysis? Everything you need to know about it
READ MOREWhat is Managerial Economics? Definition, Types, Nature, Principles, and Scope
READ MORE5 Factors Affecting the Price Elasticity of Demand (PED)
READ MORE6 Major Branches of Artificial Intelligence (AI)
READ MOREScope of Managerial Economics
READ MOREDijkstra’s Algorithm: The Shortest Path Algorithm
READ MOREDifferent Types of Research Methods
READ MORE
Latest Comments