These are the basic control-flow constructs of the R language. They function in much the same way as control statements in any Algol-like language. They are all reserved words.
if(cond) expr
if(cond) cons.expr else alt.expr<p></p><p>for(var in seq) expr
while(cond) expr
repeat expr
break
next</p>
| Parameter | Description |
|---|---|
cond |
A length-one logical vector that is not NA. Conditions of length greater than one are currently accepted with a warning, but only the first element is used. An error is signalled instead when the environment variable _R_CHECK_LENGTH_1_CONDITION_ is set to true. Other types are coerced to logical if possible, ignoring any class. |
var |
A syntactical name for a variable. |
seq |
An expression evaluating to a vector (including a list and an expression) or to a pairlist or NULL. A factor value will be coerced to a character vector. |
expr, cons.expr, alt.expr |
An expression in a formal sense. This is either a simple expression or a so called compound expression, usually of the form { expr1 ; expr2 }. |
# NOT RUN {
for(i in 1:5) print(1:i)
for(n in c(2,5,10,20,50)) {
x <- stats::rnorm(n)
cat(n, ": ", sum(x^2), "\n", sep = "")
}
f <- factor(sample(letters[1:5], 10, replace = TRUE))
for(i in unique(f)) print(i)
# }