2016-02-26 70 views
3

我正在閱讀R語言手冊,並想知道循環函數返回什麼值。 該手冊冊第3.3.2說循環:循環函數在R中返回什麼值

三個報表[供,同時,重複]返回評價的最後一個語句的值的每一個。 ... 循環語句返回的值始終爲NULL,並且不可見地返回。

那麼什麼是返回值,NULL或最後陳述中循環計算的值?

問候, 奧利弗

+0

閱讀[這](http://stackoverflow.com/questions/11653127/what-does-the-function-invisible-do)第一 – mtoto

+0

誰可以downvoted這個問題,請解釋爲什麼? –

+0

[that](http://stackoverflow.com/questions/11653127/what-does-the-function-invisible-do)只解釋了爲什麼在執行循環時不打印NULL。 –

回答

8

你說的是這個: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中)。看起來行爲發生了變化,文件可能沒有被充分清理。

[email protected]:~$ diff R-lang.2.9.0.texi R-lang.3.0.0.texi > R-lang.diff 
[email protected]:~$ 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

  • 更新:錯誤被確認,修復並報告狀態現在已關閉:已修復。

發現!