It is possible to have more than one else-if statement and/or use nested statements.
x <-122 if(x < 10) { print(‘less than ten’)} else if(x <100) { print(‘between10 and100 ’)} else if(x <1000) { print(‘between 100 and 1000’) } else{ print(‘bigger than1000 (or equal to1000 )’)} ## [1] “between 10 and 1000”
Note that the statements do not necessarily have to be encapsulated by curly brackets if the statement only takes one line.
x <-TRUE y <-pi y <- if(x) 1 else2 y # y is now 1## [1] 1
Note that hybrid forms are possible, but it gets confusing very fast. In the following piece of code the variable y
will not get the value one, but rather six.
z <-y <- if(x) {1; z <-6} else2 y # y is now 6## [1] 6 z # z is also 6## [1] 6
4.5.1.2 The Vectorised If-statement
The function ifelse()
is the vectorised version of the if-function. It allows to use vectors as input and output. While the if-statement is useful for controlling flow in code, the ifelse-function handy for data manipulation.
ifelse()
x <-1 :6 ifelse(x %%2 ==0, ‘even’, ‘odd’) ## [1] “odd” “even” “odd” “even” “odd” “even”
The ifelse
function can also use vectors as parameters in the output.
x <-1 :6 y <-LETTERS[1 :3] ifelse(x %%2 ==0, ‘even’, y) ## [1] “A” “even” “C” “even” “B” “even” # Note that y gets recycled!
4.5.1.3 The Switch-statement
An if-else construct that assigns one value to a variable based on one other variable can be condensed via the switch()
function.
switch()
x <-‘b’ x_info <- switch(x, ‘a’ = “One”, ‘b’ = “Two”, ‘c’ = “Three”, stop(“Error: invalid `x` value”) ) # x_info should be ‘two’ now:x_info ## [1] “Two”
The switch statement can always be written a an else-if statement. The following code does the same as the aforementioned code.
x <-‘b’ x_info <- if(x ==‘a’ ) { “One” } else if(x ==‘b’) { “Two” } else if(x ==‘c’) { “Three” } else{ stop(“Error: invalid `x` value”) } # x_info should be ‘two’ now:x_info ## [1] “Two”
The switch()
statement can always be written as with the if-else-if construction, which in its turn can always be written based on with if-else statements. This is same logic also applies for loops (that repeat parts of code). All loop constructs can always be written with a for-loop and an if-statement, but more advanced structures help to keep code clean and readable.
One of the most common constructs in programming is repeating a certain block of code a few times. This repeating of code is a “loop.” Loops can repeat code a fixed number of times or do this only when certain conditions are fulfilled.
for
loop – for
As in most programming languages, there is a “for-loop” that repeats a certain block of code a given number of times. Interestingly, the counter does not have to follow a pre-defined increment, the counter will rather follow the values supplied in a vector. R's for-loop is an important tool to add to your toolbox.
The for-loop is useful to repeat a block of code a certain number of times. R will iterate a given variable through elements of a vector.
for(value invector) { statements }
The for-loop will execute the statements for each value in the given vector.
x <-LETTERS[1 :5] for( j inx) { print(j) } ## [1] “A” ## [1] “B” ## [1] “C” ## [1] “D” ## [1] “E”
Note – No counter in the for loop
Unlike most computer languages, R does not need to use a “counter” to use a for-loop.
x <- c(0, 1, -1, 102, 102) for( j inx) { print(j) } ## [1] 0 ## [1] 1 ## [1] -1 ## [1] 102 ## [1] 102
repeat()
The repeat-loop will repeat the block of commands till it executes the break
command.
loop – repeat
Function use for repeat()
repeat{ commands if(condition) { break} }
x <- c(1,2) c <-2 repeat{ print(x +c) c <-c +1 if(c >4) { break} } ## [1] 3 4 ## [1] 4 5 ## [1] 5 6
Warning – Break out of he repeat loop
Do not forget the {break} statement, it is integral part of the repeat loop.
The while-loop is similar to the repeat-loop. However, the while-loop will first check the condition and then run the code to be repeated. So, this code might not be executed at all.
while
loop – while
while(test_expression) { statement }
The statements are executed as long the test_expression is TRUE.
x <- c(1,2); c <-2 while(c <4) { print(x +c) c <-c +1 } ## [1] 3 4 ## [1] 4 5
4.5.2.4 Loop Control Statements
When the break
statement is encountered inside a loop, that loop is immediately terminated and program control resumes at the next statement following the loop.
break
v <- c(1 :5) for(j inv) { if(j ==3) { print(“--break--”) break} print(j) } ## [1] 1 ## [1] 2 ## [1] “--break--”
The next
statement will skip the remainder of the current iteration of a loop and starts next iteration of the loop.
v <- c(1 :5) for(j inv) { if(j ==3) { print(“--skip--”) next} print(j) } ## [1] 1 ## [1] 2 ## [1] “--skip--” ## [1] 4 ## [1] 5
Читать дальше