title: What is the best way to run a loop of regressions in R?
body: <p>Assume that I have sources of data X and Y that are indexable, say matrices. And I want to run a set of independent regressions and store the result. My initial approach would be</p>
  
  <pre><code>results = matrix(nrow=nrow(X), ncol=(2))
  for(i in 1:ncol(X)) {
          matrix[i,] = coefficients(lm(Y[i,] ~ X[i,])
  
  }
  </code></pre>
  
  <p>But, loops are bad, so I could do it with lapply as</p>
  
  <pre><code>out &lt;- lapply(1:nrow(X), function(i) { coefficients(lm(Y[i,] ~ X[i,])) } )
  </code></pre>
  
  <p>Is there a better way to do this?</p>