为什么 R 对象不在函数或“for"中打印?环形? [英] Why do R objects not print in a function or a "for" loop?

查看:22
本文介绍了为什么 R 对象不在函数或“for"中打印?环形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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) 

对 Shapiro Wilk、Anderson Darling 和 Stem 的调用都有效,并提取了相同的列.

The calls to Shapiro Wilk, Anderson Darling, and stem all work, and extract the same column.

如果我将此代码放入for"循环中,则对 Shapiro Wilk 和 Anderson Darling 的调用将停止工作,而词干 &叶调用和打印调用继续工作.

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?

推荐答案

在循环中,自动打印被关闭,因为它是在函数内部.如果您想查看输出,则在这两种情况下都需要明确地print 某些内容.你得到的 [1] 9 东西是因为你显式打印了 y 的值.

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 of y.

这里有一个示例,说明您可能要考虑如何执行此操作.

Here is an example of how you might want to consider going about doing this.

> DF <- data.frame(A = rnorm(100), B = rlnorm(100))
> y <- 1
> shapiro.test(DF[,y])

    Shapiro-Wilk normality test

data:  DF[, y] 
W = 0.9891, p-value = 0.5895

所以我们有自动打印.在循环中,我们必须这样做:

So we have automatic printing. In the loop we would have to do this:

for(y in 1:2) {
    print(shapiro.test(DF[,y]))
}

如果你想打印更多的测试,那么只需将它们添加为循环中的额外行:

If you want to print more tests out, then just add them as extra lines in the loop:

for(y in 1:2) {
    writeLines(paste("Shapiro Wilks Test for column", y))
    print(shapiro.test(DF[,y]))
    writeLines(paste("Anderson Darling Test for column", y))
    print(ad.test(DF[,y]))
}

但这并不是很吸引人,除非您喜欢阅读大量输出.相反,为什么不保存拟合的测试对象,然后您可以打印它们并调查它们,甚至可以处理它们以将测试统计量和 p 值聚合到表格中?您可以使用循环来做到这一点:

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:

## object of save fitted objects in
obj <- vector(mode = "list", length = 2)
## loop
for(y in seq_along(obj)) {
    obj[[y]] <- shapiro.test(DF[,y])
}

然后我们可以使用

> obj[[1]]

    Shapiro-Wilk normality test

data:  DF[, y] 
W = 0.9891, p-value = 0.5895

例如,或者使用 lapply,它负责设置我们用来为我们存储结果的对象:

for example, or using lapply, which takes care of setting up the object we use to store the results for us:

> obj2 <- lapply(DF, shapiro.test)
> obj2[[1]]

    Shapiro-Wilk normality test

data:  X[[1L]] 
W = 0.9891, p-value = 0.5895

现在说我要提取Wp-value数据,我们可以处理存储所有结果的对象来提取我们想要的位,例如:

Say now I wanted to extract the W and p-value data, we can process the object storing all the results to extract the bits we want, e.g.:

> tab <- t(sapply(obj2, function(x) c(x$statistic, x$p.value)))
> colnames(tab) <- c("W", "p.value")
> tab
          W      p.value
A 0.9890621 5.894563e-01
B 0.4589731 1.754559e-17

或者对于那些喜欢重要明星的人:

Or for those with a penchant for significance stars:

> tab2 <- lapply(obj2, function(x) c(W = unname(x$statistic), 
+                                    `p.value` = x$p.value))
> tab2 <- data.frame(do.call(rbind, tab2))
> printCoefmat(tab2, has.Pvalue = TRUE)
       W p.value    
A 0.9891  0.5895    
B 0.4590  <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

这一定比将输出发射到屏幕上然后你必须倾泻而出更好吗?

This has got to be better than firing output to the screen that you then have to pour through?

这篇关于为什么 R 对象不在函数或“for"中打印?环形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆