title: R Function works on its own, error when used within lapply
body: <p>I need to perform a univariate logistic regression on all variables in my dataframe. I have 166 variables, and I have been trying to use lapply to simplify this process. However, I keep getting the error:</p>
  
  <pre><code>&gt; lapply(data$Gates, FUN=Lmodel)
  
  Error in model.frame.default(formula = Sstatus ~ x, data = data,
  na.action = na.exclude,  : 
  variable lengths differ (found for 'x') 
  </code></pre>
  
  <p>I built the function Lmodel like so:</p>
  
  <pre><code>Lmodel&lt;-function(x){
  (glm(Sstatus~x, data=data, family="binomial"))
  }
  </code></pre>
  
  <p>The function works when not used in conjunction with lapply:</p>
  
  <pre><code>&gt; Lmodel(data$Gates)
  
  Call:  glm(formula = Sstatus ~ x, family = "binomial", data = data, 
  na.action = na.exclude)
  
  Coefficients:
  (Intercept)           xy  
   2.5986      -0.6527  
  
  Degrees of Freedom: 169 Total (i.e. Null);  168 Residual
  (8 observations deleted due to missingness)
  Null Deviance:      96.72 
  Residual Deviance: 95.57    AIC: 99.57
  </code></pre>
  
  <p>My dependent variable Sstatus does contain some missing values, and I am thinking this is where my problem is. However, I don't understand why the function works on its own, but not when used with lapply. How can I fix this?</p>