0

I want to perform a 1-factor ANOVA with 4 levels, ie is.ctl, II, III, and IV.

# Tissue grades
is.ctl <- grepl("^NB", names(dat))
II <- grepl("^II", names(dat))
III <- grepl("^III", names(dat))
IV <- grepl("^IV", names(dat))

#1-factor ANOVA with 4 levels
aov.all.genes <- function(x,s1,s2,s3,s4) {
  x1 <- as.numeric(x[s1])
  x2 <- as.numeric(x[s2])
  x3 <- as.numeric(x[s3])
  x4 <- as.numeric(x[s4])
  fac <- c(rep("A",length(x1)), rep("B",length(x2)), rep("C",length(x3)), rep("D",length(x4)))
  a.dat <- data.frame(as.factor(fac),c(x1,x2,x3,x4))
  names(a.dat) <- c("factor","express")
  p.out <- summary(aov(express~factor, a.dat))[[1]][1,5]
  #p.out <- summary(aov(express~factor, a.dat))[[1]][1,4]    # use to get F-statistic
  return(p.out)
}

#run AVOVA function
dat.log <- log2(dat)

aov.pv <- apply(dat.log, 1, aov.all.genes,s1=is.ctl,s2=II,s3=III,s4=IV)

Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels

Since dput of a dataframe subset did not reproduce the error, I'm attaching a link to the full data: https://drive.google.com/file/d/1i_ZysUdpQKTLNoRxkHW_UNs5SqTRyaP0/view?usp=sharing

melil
  • 81
  • 8
  • @RuiBarradas I tried and got `Error in x[[s1]] : attempt to select more than one element in vectorIndex` ` – melil Aug 14 '21 at 07:44
  • Forget my comment, I hadn't run your code. Now that I have, I cannot reproduce the error, it works. Maybe there is something in your R session, can you try to end and restart R? – Rui Barradas Aug 14 '21 at 08:15
  • The output `aov.pv` is `c(`1007_s_at` = 0.278657506522954, `1053_at` = 0.0075703397841925, `117_at` = 0.720256265840592)`. – Rui Barradas Aug 14 '21 at 08:25
  • @RuiBarradas Since dput of a dataframe subset did not reproduce the error, I'm attaching a link to the full data: https://drive.google.com/file/d/1i_ZysUdpQKTLNoRxkHW_UNs5SqTRyaP0/view?usp=sharing – melil Aug 14 '21 at 09:29

0 Answers0