码迷,mamicode.com
首页 > 其他好文 > 详细

R流程控制

时间:2017-12-11 23:08:11      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:控制结构   停止   表达式计算   not   ssi   示例   单击   布尔   命名   

R语言if语句

一个if语句由一个布尔表达式,后跟一个或多个语句组成。

语法

在R语言中创建if语句的基本语法是 -

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true.
}

如果布尔表达式的值为真(true),则if语句中的代码块将被执行。如果布尔表达式的计算结果为假(false),则if语句结束后的第一组代码(在关闭大括号之后)将被执行。

示例

x <- 30L
if(is.integer(x)) {
   print("X is an Integer")
}

当上述代码被编译和执行时,它产生以下结果 -

[1] "X is an Integer"

R语言if..else语句

一个if语句可以跟随一个可选的else语句,当布尔表达式为false时执行else语句中的语句块代码。

语法

在R语言中创建if..else语句的基本语法是 -

if(boolean_expression) {
   // statement(s) will execute if the boolean expression is true.
} else {
   // statement(s) will execute if the boolean expression is false.
}

如果布尔表达式求值为真(true),那么将执行if语句中的代码块,否则将执行else语句中的代码块。

示例

x <- c("what","is","truth")

if("Truth" %in% x) {
   print("Truth is found")
} else {
   print("Truth is not found")
}

当上述代码被编译和执行时,它产生以下结果 -

[1] "Truth is not found"

注:这里 “Truth” 和 “truth” 是两个不同的字符串。

R语言if…else if…else语句

一个if语句可以跟随一个可选的else if...else语句,这对使用单个if...else else语句来测试各种条件非常有用。

当使用ifelse if, else语句时要注意几点。

  • if语句可以有零个或一个else,但如果有else if语句,那么else语句必须在else if语句之后。
  • if语句可以有零或多else if语句,else if语句必须放在else语句之前。
  • 当有一个else if条件测试成功,其余的else...ifelse将不会被测试。

语法

在R中创建if...else if...else语句的基本语法是 -

if(boolean_expression 1) {
   // Executes when the boolean expression 1 is true.
} else if( boolean_expression 2) {
   // Executes when the boolean expression 2 is true.
} else if( boolean_expression 3) {
   // Executes when the boolean expression 3 is true.
} else {
   // executes when none of the above condition is true.
}

示例代码

x <- c("what","is","truth")

if("Truth" %in% x) {
   print("Truth is found the first time")
} else if ("truth" %in% x) {
   print("truth is found the second time")
} else {
   print("No truth found")
}

执行上面示例代码,得到以下结果 -

[1] "truth is found the second time"

R语言switch语句

switch语句允许测试一个变量,与一个列表中的值相比较。 每个值被称为情况(case),并且对于每种情况检查被接通开关变量。

语法

在R语言中创建switch语句的基本语法是 -

switch(expression, case1, case2, case3....)

以下规则适用于switch语句

  • 如果表达式的值不是字符串,则被强制转化为整数。
  • switch内可有任意数量的case语句。 每个case语句后跟要比较的值和冒号。
  • 如果整数的值在1nargs() - 1(最大参数数)之间,则对条件的相应元素进行求值并返回结果。
  • 如果表达式计算为字符串,则该字符串与元素的名称匹配(正好)。
  • 如果有多个匹配,则返回第一个匹配元素。
  • 没有默认参数可使用。
  • 在不匹配的情况下,如果有一个未命名的元素,则返回其值。(如果有多个此类参数返回错误)。

示例代码

x <- switch(
   3,
   "first",
   "second",
   "third",
   "fourth"
)
print(x)

执行上面示例代码,得到以下结果 -

[1] "third"

R语言repeat循环

重复(repeat)循环一次又一次执行相同的代码,直到满足停止条件。

语法

在R语言中创建重复(repeat)循环的基本语法是

repeat { 
   commands 
   if(condition) {
      break
   }
}

示例

v <- c("Hello","loop")
cnt <- 2

repeat {
   print(v)
   cnt <- cnt+1

   if(cnt > 5) {
      break
   }
}

当上述代码被编译和执行时,它产生以下结果 -

[1] "Hello" "loop" 
[1] "Hello" "loop" 
[1] "Hello" "loop" 
[1] "Hello" "loop"

R语言while循环

while循环将一遍又一遍地执行相同的代码,直到满足停止条件。

语法

在R语言中创建while循环的基本语法是 -

while (test_expression) {
   statement
}

示例

v <- c("Hello","while loop")
cnt <- 2

while (cnt < 7) {
   print(v)
   cnt = cnt + 1
}

当上述代码被编译和执行时,它产生以下结果 -

[1] "Hello"  "while loop"
[1] "Hello"  "while loop"
[1] "Hello"  "while loop"
[1] "Hello"  "while loop"
[1] "Hello"  "while loop"

R语言for循环

for循环是一种重复控制结构,可以让您有效地编写一个需要执行特定次数的循环。

语法

在R语言中创建for循环的基本语法是 -

for (value in vector) {
   statements
}

R编程中的for循环特别灵活,因为它们不限于整数,甚至不限于输入的数字。 我们可以传递字符向量,逻辑向量,列表或表达式。

示例

v <- LETTERS[1:4]
for ( i in v) {
   print(i)
}

当上述代码被编译和执行时,它产生以下结果 -

[1] "A"
[1] "B"
[1] "C"
[1] "D"

循环控制语句

循环控制语句用于更改程序正常执行顺序。 当执行离开范围时,在该范围内创建的所有自动对象都将被销毁。

R支持以下控制语句。可单击以下链接来查看其详细信息。

序号 控制语句 描述
1 break语句 终止循环语句并将执行转移到循环之后的语句。
2 next语句 next语句模拟R语言中的switch语句的行为。相当于c中的continue

R流程控制

标签:控制结构   停止   表达式计算   not   ssi   示例   单击   布尔   命名   

原文地址:http://www.cnblogs.com/oneTOinf/p/8025245.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!