title: Running multiple linear regressions across several columns of a data frame in R
body: <p>I have a dataset structured as such:
  <a href="https://i.stack.imgur.com/miXxx.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>I would like to run linear regression models and ANOVA using V1, V2...etc. as the independent variables and the g column as the dependent variable in each case (i.e. lm(V1 ~ g), lm(V2 ~ g), and so forth). This would be straightforward except that these linear regressions need to be grouped by level in the pair column, such that, for example, my output contains lm(V1 ~ g) for all rows with pair 1.1 and lm(V1 ~ g) for all pairs 1.201, etc.</p>
  
  <p>I've tried a number of approaches using for loops, lapply and the data.table package, and nothing gives me exactly the output I'd like. Can anyone give me any insight on the best way to tackle this problem?</p>
  
  <p>Edit:
  My full data set has 7056 different pairs in the pair column and 100 V columns (V1...V100). My latest attempt at this problem:</p>
  
  <pre><code>df$pair &lt;- as.factor(df$pair)
  out &lt;- list()
  for (i in 3:ncol(df)){
      out[[i]] &lt;- lapply(levels(df$pair), function(x) {
      data.frame(df=x, g = coef(summary(lm(df[,i]~ df$g, data=df[df$pair==x,])),row.names=NULL))})
      }
  </code></pre>