R applies to data groups

I need to run ANOVA on each question individually. I have a dataframe composed of data coming from 37 subjects, and I don't want to loop 37 times to run an ANOVA 37 times individually for each question. These are the first 4 lines of my data:

        latency subject     trialcode
   1    1385    14233664    neighbour
   2    2493    14233664    neighbour
   3    1429    14233664    neighbour
   4    1884    14233664    neighbour

      

This is my code:

 tmp <- with(as.data.frame(data),
        by(data$subject,
           function(x) aov(latency ~ trialcode + Error(trialcode), data=data)))
 sapply(tmp, coef)

      

But I am getting the error:

Error in unique.default(x, nmax = nmax) : 

      

unique () only applies to vectors

Any help is appreciated Thanks

+3


source to share


1 answer


I think the challenge is by

wrong. If i usedata(npk)

do.call(rbind,by(npk[,-1], npk$block,
      FUN=function(x) coef(aov(yield~N+P+K, data=x))))

      

For your data, it could be



do.call(rbind, by(data[,-2], data$subject, 
     FUN=function(x) coef(aov(latency ~ trialcode+ Error(trialcode), x))))

      

Or using data.table

library(data.table)
setkey(setDT(data), subject)[, as.list(coef(aov(latency~trialcode+
           Error(trialcode))),by=subject]

      

0


source







All Articles