{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter6

# Chapter6 - Chapter 6 R Chapter 6 Advanced Programming in R...

This preview shows pages 1–4. Sign up to view the full content.

Chapter 6 R - 1 - Chapter 6 Advanced Programming in R Now that you have learned the elementary commands in R and many ways of applying them, it is time to discover its advanced functionalities. This chapter introduces grouped expressions, conditional execution, loops, and deals more intensively with writing functions. 6.1 Grouped expressions R is an expression language in the sense that its only command type is a function or expression which returns a result. Even an assignment is an expression whose result is the value assigned, and it may be used wherever any expression may be used; in particular multiple assignments are possible. Commands may be grouped together in braces, {expr1 ; expr2 ;...; exprm} in which case the value of the group is the result of the last expression in the group evaluated. Since such a group is also an expression it may, for example, be itself included in parentheses and used a part of an even larger expression, and so on. 6.2 Conditional execution: if statements 1. The language has available a conditional construction of the form > if (expr_1) expr_2 else expr_3 where expr_1 must evaluate to a single logical value and the result of the entire expression is then evident. 2. Here is an example if an “if” statement: > if (x < 3) print("x less than 3") else print ("x not less than 3")

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Chapter 6 R - 2 - 3. The “short-circuit” operators && and || are often used as part of the condition in an if statement. Whereas & and | apply element-wise to vectors, && and || apply to vectors of length one, and only evaluate their second argument if necessary. 4. If you want statement 1 and / or statement 2 to consist of more than one statement, then the if construct looks like this: > if (condition) { statement 1a statement 1b statement 1c } else { statement 2a statement 2b statement 2c } The group of statements between a { and a } are treated as one statement by the if and else . 6. There is a vectorized version of the if/else construct, the ifelse() function. This has the form ifelse(condition, a, b) and returns a vector of the length of its longest argument, with elements a[i] if condition[i] is true, otherwise b[i] . x <- c(1,2,3) y <- c(4,5,2) (x>2)&(y>1) (x>2)&&(y>1)
Chapter 6 R - 3 - 6.3 Iterations An iteration is, in principle, a loop or repeatedly executed instruction cycle, with only a few changes in each cycle. In programming languages that are not matrix or array-oriented, like C, Fortran, even a simple matrix multiplication needs three nested loops (over rows, columns, and the indices). Since R is matrix-oriented, these operations are much more efficient and easy to formulate in mathematical terms. This means they are faster than loops and the code is much easier to read and write.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}