Program Flow – Using Conditional Statements

June 21st, 2009

The R environment is based on providing access to the R programming language, which like other programming languages has a variety of constructions that can be used to control the flow of the analysis to make various decisions based on testing conditions. For each, in a function for numerical optimisation we might be interested in specifying a tolerance level to determine when the algorithm should stop or a maximum number of iterations.

The if statement can be used to evaluate a simple or complicated expression and then react based on whether the condition evaluates to TRUE or FALSE. The syntax for the if statement specifies the condition in round brackets, where the condition is a valid R statement, and the code immediately following the line is evaluated if the condition is TRUE.

As an example, we might have a variable condition1 that has been evaluated earlier in our program and we want to print out a message if this variable is TRUE. If there are multiple lines of code to be evaluated then we would use curly brackets to specify the commands to be evaluated – and in fact it is good practice to get used to using them if there is only a single line. An example of code would be:

if (condition1 == TRUE)
{
    print("Condition Satisfied")
}

If condition1 is true then the output is:

[1] "Condition Satisfied"

If we want some alternative code to be run if the condition is FALSE then we use the else statement. As an example we could write the following code:

if (condition2 == TRUE)
{
    print("Condition evaluated to True")
} else {
    print("Condition evaluated to False")
}

If the variable condition2 is TRUE then the first statement is printed, otherwise the second statement is printed to the console. Note that the else statement appears on the same line as the closing curly bracket for the true condition.

The if and else construct can be combined into a single function ifelse, which is vectorised, meaning that the function can evaluate the same condition on a column of data and it returns a column of responses. A condition is specified as well as some code to be evaluated if the condition is TRUE and also some code to be evaluated if the condition is FALSE. As a trivial example we could create a factor variable from a numeric variable by specifying a condition to divide the data into two groups. The two outcomes correspond to the value returned for based on whether the condition is TRUE or FALSE. An example of using this construct

ifelse(CO2$conc > 800, "> 800", "<= 800")

to give

 [1] "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800"
[12] "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800"
[23] "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800"
[34] "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800"
[45] "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800"
[56] "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800"
[67] "<= 800" "<= 800" "<= 800" "> 800"  "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800" 
[78] "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "<= 800" "> 800"

There are many uses for this construction that can assist with data processing.

Comments are closed.