在R中的递归函数:如何将每次调用的输出保存到一个向量中?

huangapple go评论68阅读模式
英文:

Recursive functions in R: How do I save the output of every call to a vector?

问题

如何保存每次调用vector函数的输出结果?我已经看到类似的问题,但要么表述不清晰,要么涉及更复杂的问题。对我以及其他初学者来说,看到最基本情况的解决方案将会很有帮助。

例如:

recursive = function(x) {
  if (x < 1) {
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)

[1] 0.1
[1] 0.2
[1] 0.3
[1] 0.4
[1] 0.5
[1] 0.6
[1] 0.7
[1] 0.8
[1] 0.9
[1] 1
[1] 1.1

这些输出如何直接编译成一个向量?

我尝试将它们追加到一个空向量中,但R不支持这种方法。

output <- c()
recursive = function(x) {
  if (x < 1) {
    output <- append(output, x + 0.1)
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)

希望能提供一些帮助!

英文:

How can I save the output from each call to vector?
I have seen similar questions, but either poorly formulated or for more complicated problems. It would be helpful to me, and I am sure to other beginners, to see a solution for the most basic case.

For instance:

recursive = function(x) {
  if (x &lt; 1) {
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)

[1] 0.1
[1] 0.2
[1] 0.3
[1] 0.4
[1] 0.5
[1] 0.6
[1] 0.7
[1] 0.8
[1] 0.9
[1] 1
[1] 1.1

How can these outputs be directly compiled to a vector?

I tried appending to an empty vector, but R disapproved of this approach.

output &lt;- c()
recursive = function(x) {
  if (x &lt; 1) {
    output &lt;- append(output, x + 0.1)
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)


Any help would be much appreciated!

答案1

得分: 2

你可以使用 _super assignment_ 中的 <<-。请参阅 帮助页面,以及更详细的信息,请查看《Advanced R》的 7.2.4 节

基本上,函数内部创建/修改的变量不会传递到主环境。要实现这一点,你需要使用 <<-

output <<- c()
recursive = function(x) {
  if (x < 1) {
    output <<- append(output, x + 0.1)
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)
output

 [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

编辑: 正如Onyambu指出的,使用 <<- 是危险的,可能导致代码产生意外的副作用。我建议采纳他的建议!

英文:

You can use the super assignment &lt;&lt;-. See the the help page and, for more in depth info, the section 7.2.4 of "Advanced R".

Basically, variables created/modified inside a function don't go to the main environment. To achieve that, you need &lt;&lt;-.

output &lt;- c()
recursive = function(x) {
  if (x &lt; 1) {
    output &lt;&lt;- append(output, x + 0.1)
    print(x + 0.1)
    return(recursive(x + 0.1))
  }
}
recursive(0)
output

 [1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

Edit: As Onyambu pointed out, using &lt;&lt;- is dangerous and can lead to unwanted side effects to your code. I recommend going with his answer!

答案2

得分: 2

以下是您要翻译的内容:

您可以简单地连接输出。注意:在使用 &lt;&lt;- 时要小心。

recursive = function(x) {
   if (x &lt; 1)  c(x, recursive(x + 0.1))
   else x
 }
 
 recursive(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

如果您需要明确使用 return 函数,您不需要使用 else

recursive1 &lt;- function(x) {
   if (x &lt; 1)  return(c(x, recursive1(x + 0.1)))
   x
 }
 recursive1(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

另一种方法是更改条件:

recursive2 &lt;- function(x) {
   if (x &gt; 1)  return(x)
   c(x, Recall(x + 0.1))
 }
 recursive2(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1
英文:

You can simply concatenate the output. Note: Be careful on the use of &lt;&lt;-.

recursive = function(x) {
   if (x &lt; 1)  c(x, recursive(x + 0.1))
   else x
 }

 recursive(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

If you need to explicitly use the return function, you do not need to use else:

recursive1 &lt;- function(x) {
   if (x &lt; 1)  return(c(x, recursive1(x + 0.1)))
   x
 }
 recursive1(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

Another way is to change the condition:

recursive2 &lt;- function(x) {
   if (x &gt; 1)  return(x)
   c(x, Recall(x + 0.1))
 }
 recursive2(0)
 [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1

huangapple
  • 本文由 发表于 2023年5月18日 06:57:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定