0

I'm trying to use the lm package to look at how a continuous variable is influenced by a 2-level factor. I've made sure R sees the factor as a factor (it does), and that it sees 2 levels of the factor (it does). However, when I try to run the model, I get the following error message:

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

Below is the code I used to read in my data, exclude empty cells and read data as factors. Does anyone have a sense of why I am getting an error message saying that my factor does not have 2 or more levels?

`amfcol <- read.csv(file="amfcol.csv", na.strings = c(" ", ""),header=T)
amfcol <- na.omit(amfcol)
head(amfcol)
str(amfcol)


amfcol <- amfcol[1:141,]
amfcol$amf <- as.factor(amfcol$amf)
is.factor(amfcol$amf)
levels(amfcol$amf)
is.factor(amfcol$fert)
levels(amfcol$fert)

mod1 <- lm(arbcol~amf, data=amfcol)`

Thanks!

  • Please provide minimum information on your data. What is the output of `head(amfcol)` and `levels(amfcol$amf)`? – DJack Feb 09 '18 at 22:15
  • As @DJack mentioned, it might be a duplicate error and without your data it's tough to see. Do a `table(amfcol$amf` to see if you indeed have more than 1 factor variable. As the other post states: "If your independent variable (RHS variable) is a factor or a character taking only one value then that type of error occurs." – Kamil Feb 09 '18 at 22:36

0 Answers0