title: Applying lm() and predict() to multiple columns in a data frame
body: <p>I have an example dataset below.</p>
  
  <pre><code>train&lt;-data.frame(x1 = c(4,5,6,4,3,5), x2 = c(4,2,4,0,5,4), x3 = c(1,1,1,0,0,1),
                    x4 = c(1,0,1,1,0,0), x5 = c(0,0,0,1,1,1))
  </code></pre>
  
  <p>Suppose I want to create separate models for column <code>x3</code>, <code>x4</code>, <code>x5</code> based on column <code>x1</code> and <code>x2</code>. For example</p>
  
  <pre><code>lm1 &lt;- lm(x3 ~ x1 + x2)
  lm2 &lt;- lm(x4 ~ x1 + x2)
  lm3 &lt;- lm(x5 ~ x1 + x2) 
  </code></pre>
  
  <p>I want to then take these models and apply them to a testing set using predict, and then create a matrix that has each model outcome as a column.</p>
  
  <pre><code>test &lt;- data.frame(x1 = c(4,3,2,1,5,6), x2 = c(4,2,1,6,8,5))
  p1 &lt;- predict(lm1, newdata = test)
  p2 &lt;- predict(lm2, newdata = test)
  p3 &lt;- predict(lm3, newdata = test)
  final &lt;- cbind(p1, p2, p3)
  </code></pre>
  
  <p>This is a simplified version where you can do it step by step, the actual data is far too large. Is there a way to create a function or use a for statement to combine this into one or two steps?</p>