The post is about different control structures in R Language. In R there are different control structures to control the flow or execution of the program. The control function usually makes use of if statements or their different flavors to control the code based on condition(s). On the other hand, one can repeat some desired task in sequence or based on conditions such as repeated sampling or simulation can be performed using loops such as for loop.
Table of Contents
Control Structures in R Language
What is a Control Structure in R?
R language has some standard control structures. Many R expressions/ statements (or code blocks) can be enclosed within braces { }. Control structures define the flow of the program. However, it is more efficient to use built-in functions rather than control structures whenever possible. These allow us to control the flow of execution of a script typically inside of a function.
How many Control Statements are in R Language?
There are eight control statements in R language.
Name all of the Control Statements in R
The names of the control statements in R language are:
- if
- if-else
- for
- nested loops
- while
- repeat and break
- next
- return
In general, one can say that there are two types of control structures.
- Conditional statements (if, if-else, elif, and switch statement)
- Loops (for loop, while loop, and repeat)
What is “if” control statement in R Language?
The if statements are used when a certain condition is TRUE to perform a specific task. The syntax of an if statement is
if (test expression or condition){ statement(s) }
How if statements work, consider the following a simple example
x <- 1:20 if (sample(x, 1) <= 10){ print("Sampled x is less than 10") }
From the above example, if the randomly sampled value is less than 10, then the output of the code will be “Sampled x is less than 10”, otherwise nothing will be displayed on the screen as no else is utilized here.
What is a loop in R Language?
A loop is a way to repeat a sequence of instructions/commands under certain conditions. The loops allow us to automate parts of our code that require repetition.
What does mean by the term “Dreaded for Loop”?
In R language, many questions arose about how to accomplish different tasks without the use of a for loop. This is a situation of “Dreaded for Loop”. It is usually desired as programmers try to avoid loops at all costs to speed up their code.
Give an Example of “Dreaded for Loop”
The simplest example of “Dreaded for loop” is the use of vectorization. The vectorization in R speeds up some computation, compared to the use of loops. For example, if x and y are vectors of equal lengths, one can add/sum them as
# Vectorization x <- 1:5 y <- 5:1 z <- x + y print(z) # Using for loop for (i in length(x)){ z[i] <- x[i] + y[i] } print(z)
The addition of element-wise values of the $x$ and $y$ vectors is much faster than adding/summing using loops. Note that the use of vectorization also helps the programmers to write shorter, simpler, safer, and faster code.
What is the Purpose of Using the next statement in R?
One can use the next statement if, in a loop, the programmer/ developer wants to skip the current iteration without terminating it loop.