【问题标题】:Error in Shapiro test for multiple variables:多个变量的夏皮罗测试错误:
【发布时间】:2020-12-11 01:15:13
【问题描述】:

在对多个变量进行夏皮罗测试时,我遇到了一个奇怪的问题。它工作正常是我有一个变量。 但在多个它说错误。 例如使用虹膜数据:

library(magrittr)
iris %>% head()
iris %>% shapiro.test(Sepal.Length, Petal.Width)

> iris %>% shapiro.test(Sepal.Length, Petal.Width)
Error in shapiro.test(., Sepal.Length, Petal.Width) : 
  unused arguments (Sepal.Length, Petal.Width)

如果我想为整个数据做的话

for(i in seq(dim(iris)[2])) {
  result <- append(result, shapiro.test(data[[i]]))
}

再次出错

Error in data[[i]] : object of type 'closure' is not subsettable

谁能帮我知道可能是什么问题。 提前非常感谢。 问候, 米特拉

【问题讨论】:

  • shapiro.test() 是一个变量而不是两个。运行这个?shapiro.test() 并阅读文档。

标签: r


【解决方案1】:

您可以使用lapply 对数值变量应用测试。

cols <- sapply(iris, is.numeric)
result <- lapply(iris[cols], shapiro.test)
result

【讨论】:

    【解决方案2】:

    tidyverse

    library(dplyr)
    iris %>%
           summarise(across(where(is.numeric), ~ list(shapiro.test(.))))
    

    【讨论】:

      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2012-06-06
      • 2019-06-16
      • 1970-01-01
      • 2017-05-20
      相关资源
      最近更新 更多