Skip to content
Prev 322581 / 398500 Next

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :

You didn't show an example of how to recreate your problem, but
this shows up when a term in a model is a factor (or character) variable
with no variation in it.  E.g.,
  > d <- data.frame(y=1:10, x1=rep(3,10), x2=rep(c("Female","Male"),c(7,3)))
  > lm(y ~ x1 + x2, data=d, subset = x2=="Female")
  Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
    contrasts can be applied only to factors with 2 or more levels
You have to look through the dataset to see which column is the offender.

Numeric columns with no variation are accepted; this is just a problem with
character/factor columns:
  > lm(y ~ x1 + x2, data=d)
  
  Call:
  lm(formula = y ~ x1 + x2, data = d)
  
  Coefficients:
  (Intercept)           x1       x2Male  
            4           NA            5  

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com