【问题标题】:Is there a way to change the number of iterations on the fly in r?有没有办法在 r 中动态更改迭代次数?
【发布时间】:2017-03-19 02:47:06
【问题描述】:

例如,在循环内执行函数期间,先前迭代产生的计算值表明可以减少总体迭代次数。我的目标是能够实时减少总迭代次数。

例如,for (i in 1:max) { do computation here and figured out that max can be reduced after the third iteration and hence reduce max}

【问题讨论】:

  • 这太模糊了。给出示例代码来描述细节。
  • 您可以测试一个标准并重复跳到next迭代或使用break。阅读?"for"

标签: r iteration


【解决方案1】:

想到的最简单的答案是不要使用 for 循环。

i <- 1
MAX <- 100
while (i <= MAX){
  # do stuff
  if (condition) MAX <- 50
  i <- i + 1
}

根据您的问题的性质,可能会有更优雅的方法,但这是您所要求的直接翻译。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-05
    • 2016-10-03
    • 1970-01-01
    • 2019-06-03
    • 2019-10-17
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多