r - Drop factor from formula when less than two levels -


i want able drop term formula based on whether or not factor has less 2 levels.

my setup want run large number of same logit models different subsets of data. problem that, subsets, not factors may take 1 value.

here example:

depvar <- sample(0:1, 100, replace=t) bin1 <- rep(1, 100) cont1 <- rnorm(100) df <- data.frame(depvar=depvar, bin1=bin1, cont1=cont1) formla <- as.formula("depvar ~ bin1 + cont1") logit.reg <- glm(formla, data=df, family=binomial(link=logit)) 

i able somehow drop bin1 formula automatically when takes 1 value.

seems me simple if/else should suffice:

if(length(unique(df$bin)) > 1) {   formla <- as.formula("depvar ~ bin1 + cont1") } else {   formla <- as.formula("depvar ~ cont1") } 

note bin1 not factor in example (can use as.factor() coerce 1 though)


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -