title: R: Applying lm on every row of a dataframe using apply family
body: <p>I have a data frame   </p>
  
  <pre><code>x y z  
  1 4 6  
  2 5 7  
  3 6 8  
  4 7 9  
  5 8 10   
  </code></pre>
  
  <p>Reproducible example below:</p>
  
  <pre><code>x &lt;- c(1,2,3,4,5)
  y &lt;- c(4,5,6,7,8)
  z &lt;- c(6,7,8,9,10)
  df &lt;- data.frame(x, y, z)
  df
  </code></pre>
  
  <p>I am trying to run a linear regression using lm between lines 1:4 against 5. I am trying to use the apply family here.  I have seen other links  in SO which talk about this , but having a tough time understanding the syntax. This <a href="https://stackoverflow.com/questions/27539033/r-apply-lm-on-each-data-frame-row">link</a> was a good link, but I am having a tough time understanding the syntax. This is my attempt at it.</p>
  
  <pre><code>apply(df, 1, function(x), lm(x[1,] ~ x[5,])$coefficients)
  </code></pre>
  
  <p>I am not sure what the syntax is  to write apply such that it takes all rows.</p>
  
  <p>I would also be thankful if someone could also show me how to do the same thin but with lm on columns of a dat frame too.</p>
  
  <pre><code>df = data.frame(x=c(1:5), y= c(4:8), z=c(6:10))
  </code></pre>