title: r for loop for regression lm(y~x)
body: <p>Example:</p>
  
  <pre><code>df &lt;- data.frame(A=1:5, B=2:6, C=3:7,D=4:8,E=5:9,F=6:10)
  </code></pre>
  
  <p>I want make a regression loop lm(y,x) using like y the col 1 and 2 and like x the rest of the cols.</p>
  
  <p>my idea:</p>
  
  <pre><code>lmf &lt;- function (y,x) {
                           f &lt;- lm(y ~ x, data=df)
                           cbind(summary(f)$r.squared,summary(f)$coefficients)                  
                          }
   for(y in 1:3)
    {
      R&lt;- apply(df[,3:6], 2, lmf(y,x)); R
    }
  </code></pre>
  
  <p>error: Error in model.frame.default(formula = y ~ x, data = df, drop.unused.levels = TRUE) : 
    variable lengths differ (found for 'x')</p>
  
  <p>I give this example very small but my data are 50 cols for the y and 300 cols for the x.  </p>
  
  <p>What I want is the same to do: lm(df$1~df$3, data=df); lm(df$1~df$4, data=df),[...], lm(df$2~df$3, data=df)... but in automatic way. Moreover I want to extract the results $coefficients and $r.squared. </p>