For more information about variable scope and environments see Chapter 8 The

This preview shows page 93 - 96 out of 722 pages.

For more information about variable scope and environments, see Chapter 8 . The curly brace notation is translated internally as a call to the `{` function. (Note, however, that the arguments are not evaluated the same way as in a standard function.) Control Structures Nearly every operation in R can be written as a function, but it isn’t always convenient to do so. Therefore, R provides special syntax that you can use in com- mon program structures. We’ve already described two important sets of construc- tions: operators and grouping brackets. This section describes a few other key language structures and explains what they do. Conditional Statements Conditional statements take the form: if ( condition ) true_expression else false_expression or, alternatively: if ( condition ) expression Because the expressions expression , true_expression , and false_expression are not always evaluated, the function if has the type special : > typeof(`if`) [1] "special" Here are a few examples of conditional statements: > if (FALSE) "this will not be printed" > if (FALSE) "this will not be printed" else "this will be printed" [1] "this will be printed" > if (is(x, "numeric")) x/2 else print("x is not numeric") [1] 5 In R, conditional statements are not vector operations. If the condition statement is a vector of more than one logical value, then only the first item will be used. For example: > x <- 10 > y <- c(8, 10, 12, 3, 17) > if (x < y) x else y [1] 8 10 12 3 17 Warning message: In if (x < y) x else y : the condition has length > 1 and only the first element will be used Control Structures | 71 R Syntax
If you would like a vector operation, use the ifelse function instead: > a <- c("a", "a", "a", "a", "a") > b <- c("b", "b", "b", "b", "b") > ifelse(c(TRUE, FALSE, TRUE, FALSE, TRUE), a, b) [1] "a" "b" "a" "b" "a" Often, it’s convenient to return different values (or call different functions) depend- ing on a single input value. You can code these as > switcheroo.if.then <- function(x) { + if (x == "a") + "camel" + else if (x == "b") + "bear" + else if (x == "c") + "camel" + else + "moose" + } but that is verbose. A better alternative is to use the switch function: > switcheroo.switch <- function(x) { + switch(x, + a="alligator", + b="bear", + c="camel", + "moose") + } The first argument is a character value to switch on, the named arguments specify what to do for each value of the argument, and an unnamed argument specifies the default value. As you can see, these two expressions are equivalent: > switcheroo.if.then("a") [1] "camel" > switcheroo.if.then("f") [1] "moose" > switcheroo.switch("a") [1] "camel" > switcheroo.switch("f") [1] "moose" Loops There are three different looping constructs in R. Simplest is repeat , which just repeats the same expression: repeat expression To stop repeating the expression, you can use the keyword break . To skip to the next iteration in a loop, you can use the command next . As an example, the following R code prints out multiples of 5 up to 25: > i <- 5 > repeat {if (i > 25) break else {print(i); i <- i + 5;}} 72 | Chapter 6: R Syntax
[1] 5 [1] 10 [1] 15 [1] 20 [1] 25 If you do not include a break command, the R code will be an infinite loop. (This can be useful for creating an interactive application.) Another useful construction is while

#### You've reached the end of your free preview.

Want to read all 722 pages?

• Winter '17
• Saltz
Stuck? We have tutors online 24/7 who can help you get unstuck.
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes