官术网_书友最值得收藏!

  • Learning Shiny
  • Hernán G. Resnizky
  • 423字
  • 2021-07-09 21:46:11

Control structures in R

Control structures in computer programming are statements that decide the execution (or not) of certain pieces of code. In while and if, they are based on a condition that evaluates to TRUE or FALSE, and in for, the statement is executed for every element of the input sequence.

In R, all the control structures have the same coding pattern, as follows:

control_structure(condition or sequence){code block}

The if...else block

The following is a small example of an if...else block in R. You can play with it by changing the value of a:

> a <- 5
> if(a > 0){print("a is greater than 0")} else
+ { print("a is smaller than 0")}
[1] "a is greater than 0"

Note

The else clause must start in the same line where the if clause ends.

With an else...if statement, it would be:

> a <- 10
> if(a < 0 ){
+ print("a is smaller than 0")} else if(a >= 0 & a <= 5)
+ { print("a is between 0 and 5")} else
+ { print("a is greater than 5")}
[1] "a is greater than 5"

The while loop

The use of while must be well controlled, as it can lead to infinite or large loops, even causing system collapse. An example of this loop is as follows:

> while(a < 4){
+ print(paste("This is iteration",a))
+ a <- a + 1
+ }
[1] "This is iteration 1"
[1] "This is iteration 2"
[1] "This is iteration 3"

The for loop

The for loop is a special form of control structure as it does not require an explicit condition. However, it is implicitly given in the length of the sequence passed, that is, it will stop when it comes to its last element in the vector that is passed. These vectors can be of any class.

The following is a loop over a character vector:

> vector <- c("aaa","bbb","ccc")
> for(i in vector){
+ 
+ print(i)
+ 
+ }
[1] "aaa"
[1] "bbb"
[1] "ccc"

The following is a loop over a numeric vector:

> numbers <- 1:3
> for(i in numbers){
+ 
+ print(i + 2)
+ 
+ }
[1] 3
[1] 4
[1] 5

Here, i replaces the elements of the loop over the iterations during the execution of the code.

The switch statement

Although switch() is not strictly a control structure, it works in the same way as if(). In fact, it is an abbreviation of a chain of if/else...if/else statements where the condition matches an exact value. Switch has different behaviors and logic based on whether the value that is being evaluated matches a string or a number.

In the case of strings, a default value (the else statement) is allowed, while in the case of numbers, it is not. A number in the condition argument implicitly refers to an index. For example, see the following:

if(a == 1)
{ print("a is 1")} else if (a == 2)
{print("a is 2")} else if ( a == 3)
{print ("a is 3")}

This could be rewritten as shown here:

print(switch(a,"a is 1","a is 2","a is 3"))

In the case of characters, each of the cases must be explicit, except for the default, as follows:

> inp <- "b"
> switch(inp,
+ a=print("inp is a"),
+ b=print("inp is b"),
+ c=print("inp is c"))
[1] "inp is b"

The following is an example with a default case:

> inp <- "d"
> 
> switch(inp,
+ a=print("inp is a"),
+ b=print("inp is b"),
+ c=print("inp is c"),
+ print("inp is not a, b, or c"))
[1] "inp is not a, b, or c"
主站蜘蛛池模板: 茶陵县| 那坡县| 青浦区| 石棉县| 大兴区| 嘉黎县| 遂川县| 道孚县| 诏安县| 宣汉县| 文水县| 武隆县| 金昌市| 定结县| 弥渡县| 乌兰察布市| 东源县| 双峰县| 怀来县| 平江县| 梅河口市| 江永县| 抚顺县| 晋中市| 林州市| 高雄市| 肇东市| 闵行区| 堆龙德庆县| 神农架林区| 岫岩| 兖州市| 兰溪市| 阜平县| 桃源县| 华容县| 华安县| 万源市| 乡城县| 方正县| 琼海市|