运行 R 中的夏皮罗测试

Running a shapiro test in R

如果我 table 喜欢:

group1   group2    frequency
  A        B          1
  A        A          2
  A        D          4
  A        C          1
  B        B          1
  B        D          5
  B        C          6
  B        A          3
  .        .          .
  .        .          .
  .        .          .

我想 运行 夏皮罗测试组 "group1"。我想要的结果是:

group1    statistics       p.value
  A       0.9475648     1.228816e-01                                 
  B       0.7533102     6.058378e-06
  .           .               .
  .           .               .
  .           .               .

有人知道吗?

这是您要找的吗?

tab <- data.frame(group1=c("A","A","A","A","B","B","B","B"), group2=c("B","A","D","C","B","D","C","A"), frequency=c(1,2,4,1,1,5,6,3))
do.call(rbind, by(tab, tab$group1, function(x) unlist(shapiro.test(x$frequency)[c("statistic","p.value")])))

或者这个:

library(plyr)
ddply(tab, .(group1),  
    function(x) unlist(shapiro.test(x$frequency)[c("statistic","p.value")]))

dplyr

library(dplyr)
dat %>%
  group_by(group1) %>% 
  do(data.frame(shapiro.test(.$frequency)[c("statistic", "p.value")]))

#   group1 statistic   p.value
#   (fctr)     (dbl)     (dbl)
#1      A 0.8274267 0.1611906
#2      B 0.9630724 0.7982271

数据

dat <- data.frame(group1=c("A","A","A","A","B","B","B","B"), 
       group2=c("B","A","D","C","B","D","C","A"), 
       frequency=c(1,2,4,1,1,5,6,3))