Previous: Functions | Up: Programming Statements | Next: For-Loops |
Use if (and optionally, else) to control the flow of R functions. For example, let x and y be scalar numerical values:
if (x == y) { # If the logical statement in the ()'s is true, x <- NA # then `x' is changed to `NA' (missing value). } else { # The `else' statement tells R what to do if x <- x^2 # the if-statement is false. }As with a function, use { and } to define the set of commands associated with each if and else statement. (If you include if statements inside functions, you may have multiple sets of nested curly braces.)