I am trying to run a loop for an anova analysis in R. But I keep on getting this error message:
Error in contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
I have read that you can get this error if your variables have less that 2 levels or have NA's. But I have double checked and this is not an issue. It seems to be related to the "Diet" variable as I can run the code if I remove it. Does anyone know why this is?
data_long <- gather(data, key="class", value="value", -Diet, -Gender, -Weight, -Sample_ID)
head(data_long)
Weight Diet Sample_ID Gender class value
1 LOW MILK 8 F CE18.1 0
2 LOW MILK 9 F CE18.1 10
3 LOW MILK 17 F CE18.1 3
4 LOW MILK 30 M CE18.1 NA
5 LOW MILK 31 M CE18.1 1
6 LOW MILK 37 M CE18.1 5
str(data_long)
'data.frame': 35892 obs. of 6 variables:
$ Weight : Factor w/ 2 levels "LOW","HIGH": 1 1 1 1 1 1 1 1 1 1 ...
$ Diet : Factor w/ 2 levels "MILK","VEG": 1 1 1 1 1 1 1 1 1 2 ...
$ Sample_ID: Factor w/ 36 levels "1","2","3","4",..: 7 8 15 26 27 32 33 34 36 2 ...
$ Gender : Factor w/ 2 levels "F","M": 1 1 1 2 2 2 1 2 1 2 ...
$ class : Factor w/ 997 levels "CE18.1","CE18.2",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 0 10 3 NA 1 5 0 0 0 0 ...
output<-lapply(split(data_long, data_long$class), function(i){
m0<-lm(value~Diet*Weight+Gender,data=i)
m1<-lm(value~Diet+Weight+Gender,data=i)
anova(m0,m1)
})