Conditionals in R

Conditionals are expressions that perform different computations or actions depending on whether a predefined boolean condition is TRUE or FALSE.  Conditional statements include if(), the combination if()/esle(), ifelse(), and switch().  Each statement supports source code branching by altering the control flow.  

The if() Statement

The if() statement is common in all programming languages.  The if() statement performs operations based on a simple condition:

Here, condition is any expression that evaluates to a logical value, and true.expression is the command evaluated if condition is TRUE or non-zero. The else part is optional and omitting it is equivalent to using else {NULL}.  If condition has a vector value, only the first component is used and a warning is issued (see ifelse() for vectorized needs).  The expression text needs to be braced only when more than one command is specified. 

Common infix operators used in if() statements include:

  • equals: ==
  • not equal: !=
  • less than/greater than: < or >
  • (less than or equal/greater than or equal: <= or >=

Multiple Conditions and Multiple Cases of the if() Statement

It may be helpful to test multiple conditions within if() statements. You can use the two conditional operators, && and ||, for logical AND and OR statements, respectively. The syntax is:

Multiple operators can be combined over and over.  The conditionals  will evaluate only as far as necessary. 

Use of  if() and esle if()

One of the most common uses of the if() statement is to test multiple discrete cases:

The idea is to identify each case and to have one outcome per if() or else() statement. Clarity is the goal.  The example below is a simple function to generate a set of random numbers from one of three distributions:

The escape character \ is used in the last stop() message so that the double quotes are recognized and printed.

The ifelse() Statement

The ifelse() statement is a vectorized version of the if() statement. The syntax is:

The ifelse() function evaluates both expression1 and expression2 and then returns the appropriate values from each based on the element-by-element value of condition. If an element passes condition as TRUE, ifelse() returns the corresponding value of expression1; otherwise, it returns expression2.   ifelse() is very handy as it provides a method for evaluating a condition over an entire vector or array of values without resorting to a for() loop. For example:

ifelse() is much quicker than an if() statement with an imbedded for() loop, and it easily handles missing values.

Warning: The function  ifelse() is designed to work with vectors and matrices – not data frames.

The switch() Statement

The switch() function is an alternative to multiple use of the if() function.  The switch() function accepts as its first argument an expression:

  • If the expression is character object, it is compared to the remaining arguments until a match is found.  The value of the matched argument is returned.
  • If the first argument is an integer in the range (1:nargs – 1), it is compared to the to the remaining arguments until a match is found.  The value of the matched argument is returned.

For example, we can rewrite my.ran function using switch() as follows:

Abbreviated names are not matched, but it is possible to allow them by using the function pmatch().

Back | Next