title: Apply condition to multiple elements of a vector? Warning message: the condition has length > 1 and only the first element will be used
body: <p>I'm running a bunch of regression models, screening covariates for confounding, and wrote a function to make the process easier by only displaying regression results on the console if the covariate is a confounder. Below is some random, sample data to illustrate the idea:</p>
  
  <pre><code>Var1 &lt;- c(2.3940, 4.3848, 4.2840, 5.37393, 19.383948)
  Var2 &lt;- c(383, 4840, 38404, 48403, 8302)
  data &lt;- data.frame(Var1, Var2)
  attach(data)
  </code></pre>
  
  <p>One of my covariates is a factor variable with three levels:</p>
  
  <pre><code>Var3 &lt;- c(3938, 48403, 585038, 383028, 474937)
  Var4 &lt;- c(.373938, .473038, .830937, .3830938, 1.203)
  Var5 &lt;- as.factor(c("Ever", "Sometimes", "Never", "Sometimes", "Ever"))
  Covariates &lt;- data.frame(Var3, Var4, Var5)
  </code></pre>
  
  <p>And the function:</p>
  
  <pre><code>confounder &lt;- function(model) {
    model.sum &lt;- summary(model)
    model.b &lt;- model.sum$coefficients[2, 1]
    oldmodel &lt;- update(model, . ~ . - x)
    oldmodel.sum &lt;- summary(oldmodel)
    oldmodel.b &lt;- oldmodel.sum$coefficients[2, 1]
    model.frame &lt;- tidy(model)
    newvar.b &lt;- model.frame[grep("x", model.frame$term), 5]
    if (abs(model.b - oldmodel.b)/abs(model.b) &gt;= .1 | newvar.b &lt; .05) {
      return(model.sum)
    }
  }
  </code></pre>
  
  <p>Then I run it using lapply:</p>
  
  <pre><code>lapply(Covariates, function(x) {
    confounder(lm(Var1 ~ Var2 + x))
  })
  </code></pre>
  
  <p>And I get this error message:</p>
  
  <pre><code>Warning messages:
  1: In if (abs(model.b - oldmodel.b)/abs(model.b) &gt;= 0.1 | newvar.b &lt;  :
    the condition has length &gt; 1 and only the first element will be used
  2: In if (abs(model.b - oldmodel.b)/abs(model.b) &gt;= 0.1 | newvar.b &lt;  :
    the condition has length &gt; 1 and only the first element will be used
  </code></pre>
  
  <p>How do I change this function to apply the condition when I get to a factor variable with multiple levels?  Essentially, if any of the levels of the factor variable fit the condition, not just the first one, I want the console to display the regression output. Hope this makes sense.</p>
  
  <p>Thanks!</p>