在R

时间:2016-04-04 01:15:43

标签: r normal-distribution

如果我有一张像这样的表:

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"运行一个shapiro测试组。我想要的结果是:

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

是否有任何线索?

2 个答案:

答案 0 :(得分:4)

这就是你要找的东西吗?

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")]))

答案 1 :(得分:1)

使用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))