gpt4 book ai didi

r - 循环函数在 R 中返回什么值

转载 作者:行者123 更新时间:2023-12-04 11:27:34 25 4
gpt4 key购买 nike

我正在阅读 R 语言手册,想知道循环函数返回什么值。手册在第 3.3.2 节循环中说:

Each of the three statements [for, while, repeat] returns the value of the last statement that was evaluated. ... The value returned by a loop statement is always NULL and is returned invisibly.

那么返回什么值,NULL 还是在循环中评估的最后一条语句的值?

问候,奥利弗

最佳答案

你说的是这个:https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Looping

x = for(i in 1:10){ i }
i
#[1] 10
x
#NULL
x <- while(i < 20){ i=i+1 }
i
#[1] 20
x
#NULL
x <- repeat { if(i>=30){break}; i=i+1 }
i
#[1] 30
x
#NULL

非常肯定NULL。

我检查了旧版本的文档。语句“循环语句语句返回的值始终是@code{NULL}并且不可见地返回。”首先出现在 R3.0.0 中(它在 2.9.0 中不存在)。看起来行为发生了变化,并且文档可能没有被充分清理。

jicawi@JimisPC:~$ diff R-lang.2.9.0.texi R-lang.3.0.0.texi > R-lang.diff
jicawi@JimisPC:~$ grep -n NULL R-lang.diff
82:> The value returned by a loop statement statement is always @code{NULL}
...

所以,我安装了 R 2.9.0 并运行了同样的东西:

x = for(i in 1:10){ i }
x
#[1] 10
x <- while(i < 20){ i=i+1 }
x
#[1] 20
x <- repeat { if(i>=30){break}; i=i+1 }
x
#[1] 30

绝对是最后一个声明:)

已提交错误报告:https://bugs.r-project.org/bugzilla/show_bug.cgi?id=16729

  • 更新:该错误已得到确认、修复并报告状态现已关闭:已修复。

很好看!

关于r - 循环函数在 R 中返回什么值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35648290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com