为什么 R 对象不在函数或“for”中打印环形?
我有一个名为 ddd 的 R 矩阵。当我输入此内容时,一切正常:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
对 Shapiro Wilk、Anderson Darling 的调用和梗都正常工作,并提取同一列。
如果我将此代码放入“for”循环中,则对 Shapiro Wilk 和 Anderson Darling 的调用将停止工作,而leaf 调用和 print 调用继续工作。
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
如果我尝试编写一个函数,也会发生同样的事情。软件和广告不起作用。其他电话也可以。
> 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
为什么所有调用的行为方式不一样?
I have an R matrix named ddd. When I enter this, everything works fine:
i <- 1
shapiro.test(ddd[,y])
ad.test(ddd[,y])
stem(ddd[,y])
print(y)
The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.
If I put this code in a "for" loop, the calls to Shapiro Wilk, and Anderson Darling stop working, while the the stem & leaf call and the print call continue to work.
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
The same thing happens if I try and write a function. SW & AD do not work. The other calls do.
> 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
Why don't all the calls behave the same way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在循环中,自动打印被关闭,因为它位于函数内部。如果您想查看输出,则需要在这两种情况下显式地
print
某些内容。您得到的[1] 9
内容是因为您显式打印了y
的值。以下是您可能要考虑如何执行此操作的示例。
所以我们有自动打印。在循环中,我们必须这样做:
如果您想打印更多测试,则只需将它们添加为循环中的额外行:
但这不是很有吸引力,除非您喜欢阅读大量输出。相反,为什么不保存拟合的测试对象,然后打印它们并研究它们,甚至可以处理它们以将测试统计数据和 p 值聚合到表中?您可以使用循环来做到这一点:
然后我们可以使用例如或使用
lapply
查看模型,它负责设置我们用来存储结果的对象:
现在说我想要提取
W
和p-value
数据,我们可以处理存储所有结果的对象来提取我们想要的位,例如:重要性星星:
这一定比将输出发送到屏幕然后你必须倾倒更好吗?
In a loop, automatic printing is turned off, as it is inside a function. You need to explicitly
print
something in both cases if you want to see the output. The[1] 9
things you are getting is because you are explicitly printing the values ofy
.Here is an example of how you might want to consider going about doing this.
So we have automatic printing. In the loop we would have to do this:
If you want to print more tests out, then just add them as extra lines in the loop:
But that isn't very appealing unless you like reading through reams of output. Instead, why not save the fitted test objects and then you can print them and investigate them, maybe even process them to aggregate the test statistics and p-values into a table? You can do that using a loop:
We can then look at the models using
for example, or using
lapply
, which takes care of setting up the object we use to store the results for us:Say now I wanted to extract the
W
andp-value
data, we can process the object storing all the results to extract the bits we want, e.g.:Or for those with a penchant for significance stars:
This has got to be better than firing output to the screen that you then have to pour through?
不是一个新的答案,但除了上述之外:“flush.console()”对于强制在循环期间而不是之后进行打印是必要的。我在循环期间使用 print() 的唯一原因是显示进度,例如读取许多文件。
Not a new answer, but in addition to the above: "flush.console()" is necessary to force printing to take place DURING the loop rather than after. Only reason I use print() during a loop is to show progress, e.g., of reading many files.
加文·辛普森的回答非常棒。我把最后一点魔法变成了一个函数。
然后你可以用你的数据框调用它
sw.df ( df )
如果你想尝试转换:
sw.df (日志(df))
Fantastic answer from Gavin Simpson. I took the last bit of magic and turned it into a function.
Then you can just call it with your data frame
sw.df ( df )
And if you want to try a transformation:
sw.df ( log(df) )