为什么R对象不能在函数或“for”循环中打印?

为什么R对象不能在函数或“for”循环中打印?

我有一个名为ddd的R矩阵。当我输入这个,一切正常:

i <- 1shapiro.test(ddd[,y])ad.test(ddd[,y]) stem(ddd[,y]) print(y)

对Shapiro Wilk,Anderson Darling和stem的调用全部工作,并提取相同的专栏。

如果我把这段代码放在“for”循环中,那么对Shapiro Wilk和Anderson Darling的调用就会停止工作,而stem&leaf调用和打印调用将继续工作。

for (y in 7:10) {
    shapiro.test(ddd[,y])
    ad.test(ddd[,y]) 
    stem(ddd[,y]) 
    print(y)}The decimal point is 1 digit(s) to the right of the |

  0 | 0
  0 | 899999
  1 | 0[1] 7

如果我尝试编写一个函数,会发生同样的事情。SW&AD不起作用。其他的电话呢。

> D <- function (y) {+ shapiro.test(ddd[,y])+ ad.test(ddd[,y]) + stem(ddd[,y]) + print(y)  }> D(9)

  The decimal point is at the |

   9 | 000
   9 | 
  10 | 00000[1] 9

为什么不是所有的调用都以相同的方式运行?


慕侠2389804
浏览 1098回答 3
3回答

摇曳的蔷薇

这不是一个新的答案,但除了上述之外:“flush.console()”是强制打印在循环期间而不是之后发生的必要条件。我在循环期间使用print()的唯一原因是显示进度,例如,读取许多文件。for&nbsp;(i&nbsp;in&nbsp;1:10)&nbsp;{ &nbsp;&nbsp;print(i) &nbsp;&nbsp;flush.console() &nbsp;&nbsp;for(j&nbsp;in&nbsp;1:100000) &nbsp;&nbsp;&nbsp;&nbsp;k&nbsp;<-&nbsp;0}
打开App,查看更多内容
随时随地看视频慕课网APP