r - R for 循环没有通过 Grubbs 的测试打印出所需的结果
问题描述
我是 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
}
同样的结果,它什么也没打印。我不确定可能缺少什么。任何帮助,将不胜感激。谢谢
解决方案
这有帮助吗?没有检查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