在 R 中退出 for 循环 您所在的位置:网站首页 企鹅表皮干燥吗还是湿润 在 R 中退出 for 循环

在 R 中退出 for 循环

2024-03-30 06:12| 来源: 网络整理| 查看: 265

R 中的 for 循环示例 R 中的 break 关键字 使用 break 终止 R 中的嵌套 for 循环 在 R 中使用 break 关键字 结论

for 循环在 R 中有两个特点。

它迭代对象的元素。 它不返回任何东西。

要在完成与对象中元素数量一样多的迭代之前终止 for 循环,我们只有一个选择:break 关键字。

R 中的 for 循环示例

我们首先创建一个向量,因为 for 循环遍历对象的元素。for 块中的变量保存向量的当前元素的值。

for 循环的迭代次数与向量中的元素一样多。

示例代码:

# Create a vector. myVec = c("First Name", "Last Name", "Address", "Phone", "Country") # The for loop. # Here, elem is a variable that holds the current element of the vector myVec. for(elem in myVec) { print(elem) }

输出:

[1] "First Name" [1] "Last Name" [1] "Address" [1] "Phone" [1] "Country" R 中的 break 关键字

break 关键字用于在循环执行后立即退出。它通常在满足条件时使用。

在示例代码中,我们将在两个不同循环的不同位置使用 break 关键字来显示循环何时终止。

示例代码:

# Exit on the third iteration BEFORE printing the updated value of the elem variable. for(elem in myVec) { if(elem == "Address") {break} print(elem) } # Exit on the third iteration AFTER printing the updated value of the elem variable. for(elem in myVec) { print(elem) if(elem == "Address") {break} }

输出:

> # Exit on the third iteration BEFORE printing the updated value of the elem variable. > for(elem in myVec) + { + if(elem == "Address") {break} + print(elem) + } [1] "First Name" [1] "Last Name" > > # Exit on the third iteration AFTER printing the updated value of the elem variable. > for(elem in myVec) + { + print(elem) + if(elem == "Address") {break} + } [1] "First Name" [1] "Last Name" [1] "Address" 使用 break 终止 R 中的嵌套 for 循环

我们可以嵌套 for 循环。如果我们的代码在嵌套的 for 循环中执行 break 关键字,它会立即跳出嵌套循环。

在执行了 break 的循环之后,控制返回到外循环的下一行。请参阅示例代码的输出以进行说明。

我们将首先创建第二个向量来帮助说明嵌套 for 循环的中断。我们将在嵌套循环的第三次迭代中使用 break 关键字,这发生在外循环的第二次迭代中。

需要注意的一点是外循环的第三次迭代被完全执行。

示例代码:

# Create another vector. myVec2 = c("One", "Two", "Three") # Break out of the inner loop in the third iteration of the inner loop # which happens in the second iteration of the outer loop # Outer for loop. for(item in myVec2) { # Nested for loop. for(elem in myVec) { cat(item, ": ", elem, "\n") # Used in place of print(). if(item == "Two" && elem == "Address") {break} } }

输出:

One : First Name One : Last Name One : Address One : Phone One : Country Two : First Name Two : Last Name Two : Address Three : First Name Three : Last Name Three : Address Three : Phone Three : Country

我们发现嵌套循环中的 break 关键字仅在满足 if 条件时才影响该循环的执行。

外循环继续它的迭代。这是因为 break 的条件涉及来自两个循环的变量。

如果我们只使用内循环的条件,它将在每次外循环迭代时中断。内循环不会在任何外循环迭代中完全执行。

示例代码:

# Break out of the inner loop in the third iteration of the inner loop # The break will get executed in EVERY iteration of the outer loop. # Outer for loop. for(item in myVec2) { # Nested for loop. for(elem in myVec) { cat(item, ": ", elem, "\n") # Used in place of print(). if(elem == "Address") {break} } }

输出:

One : First Name One : Last Name One : Address Two : First Name Two : Last Name Two : Address Three : First Name Three : Last Name Three : Address

我们发现嵌套循环中的 break 关键字在外循环的每次迭代中都被执行,因为每次都满足 if 条件。

最后,让我们看看将 break 关键字放在外循环中的效果。我们将在示例中的嵌套循环之后立即放置关键字。

if 条件在外部循环的第二次迭代中得到满足,但 break 关键字出现在嵌套的 for 循环之后。所以嵌套循环在外循环终止之前被执行。

示例代码:

# Outer for loop. for(item in myVec2) { # Nested for loop. for(elem in myVec) { cat(item, ": ", elem, "\n") # Used in place of print(). } if(item == "Two"){break} # Gets executed at the END of the second iteration of the outer loop. }

输出:

One : First Name One : Last Name One : Address One : Phone One : Country Two : First Name Two : Last Name Two : Address Two : Phone Two : Country 在 R 中使用 break 关键字

当在 for 循环中使用 break 关键字时,我们需要使用我们的编码逻辑来确保两件事。

break 命令仅在我们想要的条件下执行。 循环的其余部分按照我们的意愿运行。

print() 语句是一个可以帮助我们完成这项任务的工具。我们可以在最内层循环或每个循环中添加 print() 语句。

检查输出将向我们展示执行 break 命令的条件。

关于 R 函数的帮助

有关 R Studio 中 R 函数或关键字的帮助,请单击 帮助 > 搜索 R 帮助,然后在搜索框中键入函数名称或关键字(不带括号)。

或者,在 R 控制台的命令提示符处键入一个问号,后跟函数或关键字名称。例如,?break。

结论

break 关键字允许我们终止 R 中的 for 循环。它只终止它执行的循环。

在循环中添加 print() 语句有助于我们确定 break 命令是否在正确的条件下执行。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有