【问题标题】:R for loop not printing out the desired result with Grubbs' test [duplicate]R for循环没有通过Grubbs的测试打印出所需的结果[重复]
【发布时间】:2021-11-09 04:10:51
【问题描述】:

我是 R 的初学者,我试图通过 R Markdown 中数据框“七项全能”的每一列运行 Grubbs 测试。所以,我使用了一个 for 循环:

for (i in 1:ncol(heptathlon)) {
  grubbs.test(heptathlon[ ,i], type = 10, opposite = FALSE, two.sided = FALSE)
}

但运行代码后它什么也不打印。我还尝试了一个while循环:

i <- 1
while (i <= ncol(heptathlon)) {
  grubbs.test(heptathlon[ ,i], type = 10, opposite = FALSE, two.sided = FALSE)
  i = i+1
}

同样的结果,它什么也不打印。我不确定可能缺少什么。任何帮助,将不胜感激。谢谢

【问题讨论】:

  • 默认情况下,循环内不会打印任何内容。如果要打印值,请使用print(grubbs.test(heptathlon[ ,i], type = 10, opposite = FALSE, two.sided = FALSE))
  • 啊,明白了。不敢相信我忽略了这一点。谢谢!

标签: r


【解决方案1】:

这有帮助吗? 尚未检查grubbs.test() 的参数。

编辑:找到相关的包和数据集:

library(HSAUR) 
library(outliers)
heptathlon <- heptathlon

grubbsT <- c()
# does not make much sense to test "score", therefore (ncol(..)-1)
for (i in 1:(ncol(heptathlon)-1)) {
  
  # store resulst in the vector 
  grubbsT[i] <- round(grubbs.test(heptathlon[ ,i], 
                                  type = 10)$p.value, digits=5) 
  # assuming the test works if you input an arbitrary column of your 
  # df, and also, you want to get the p-values   
  
}

输出:

# inspect vector of p.values
grubbsT
#> [1] 0.000 0.000 0.370 0.305 0.046 1.000 0.002

由 reprex 包 (v2.0.1) 于 2021-09-14 创建

【讨论】:

  • 由于某种原因,它产生了一个错误“要替换的项目数不是替换长度的倍数”重复几次,然后还是运行了测试。所以我想它有点工作!它没有打印出每一列的 p 值,但是,只有检验统计量 G 和 U。
  • 看到了编辑。这行得通,谢谢!
  • 我又编辑了一遍。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 2015-12-29
  • 2022-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
相关资源
最近更新 更多