user11916948
user11916948

Reputation: 954

Error in code running a function over several varaibles, where is the error?

There is some error when running the function pvalues, but I dont know whats wrong. I guess it is something with (). Can anyone see it? Or is the problem you cant run sapply over several rows? in that case, how would you do it?

set.seed(1)
  id <- rep(1:3,each=4)
  trt <- rep(c("A","OA", "B", "OB"),3)
  pointA <- sample(1:10,12, replace=TRUE)
  pointB<- sample(1:10,12, replace=TRUE)
  pointC<- sample(1:10,12, replace=TRUE)
  test <- data.frame(id,trt,pointA, pointB,pointC)
  test

  pvalues <- sapply( test[,3:5] , FUN = function(x) 
    dflmer <- lmer(test[,x] ~ (1|id) + trt, data=test
    printanov <- Anova(dflmer, type=3)
    printanov$`Pr(>Chisq)`[[2]]
    )
    data.frame(pvalues)

Upvotes: 1

Views: 32

Answers (1)

jay.sf
jay.sf

Reputation: 72984

You need curly brackets with multiple lines in an sapply. Code below should work. Also the test[,x] is redundant, use x.

pvalues <- sapply(test[,3:5], FUN = function(x) {
  dflmer <- lmer(x ~ (1|id) + trt, data=test)
  printanov <- Anova(dflmer, type=3)
  printanov$`Pr(>Chisq)`[[2]]
})

Upvotes: 1

Related Questions