Search code examples
rnormal-distribution

Running a shapiro test in R


If I got a table like:

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
  .        .          .
  .        .          .
  .        .          .

I want to run a shapiro test group by "group1". The result I want is:

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

Does anyon have a clue?


Solution

  • is this what you are looking for?

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

    or this:

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