title: Converting code from ddply to plyr in R
body: <p>I want to change a bit of my code to use plyr instead of ddply because I think it will be faster on my large (>1e6) data set. Here is an example data set:</p>
  
  <pre><code>ID &lt;- rep(1:3, each=6)
  Row &lt;- rep(1, each=18) 
  Col &lt;- rep(rep(1:2, each=3), times=3)
  Meas &lt;- rnorm(18,3,1)
  len &lt;- rep(1:3, times=6)
  
  df &lt;- data.frame(ID, Row, Col, Meas, len)
  </code></pre>
  
  <p>The code I normally use is this:</p>
  
  <pre><code>res &lt;- ddply(df, c("ID", "Row", "Col"), function(x) coefficients(lm(Meas~len,x)))
  </code></pre>
  
  <p>It performs a lm for Meas against len for each subset of df by ID, Row and Col, extracting the coefficients. On my large data set it takes 30 seconds (not the end of the world, I know). When I try plyr with this:</p>
  
  <pre><code>res2 &lt;- df %&gt;% group_by("ID", "Row", "Col") %&gt;% (function(x) coefficients(lm(Meas~len,x))) %&gt;%
    as.data.frame()
  </code></pre>
  
  <p>I only get one intercept and grad. I've read this (<a href="https://stackoverflow.com/questions/32274779/extracting-p-values-from-multiple-linear-regression-lm-inside-of-a-ddply-funct">extracting p values from multiple linear regression (lm) inside of a ddply function using spatial data</a>) which gave me this attempt:</p>
  
  <pre><code>res3 &lt;- df %&gt;% group_by("ID", "Row", "Col") %&gt;%
    do({model=lm(Meas~len, data=.)
    data.frame(tidy(model),
               glance(model))})
  </code></pre>
  
  <p>But again no luck. I'm sure I'm missing something simple.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Out of interest for anyone running a similar thing on large data sets:</p>
  
  <pre><code>system.time(
  lres &lt;- ddply(I, c("ERF", "Wafer", "Row", "Col"), function(x) coefficients(lm(Rds.on.fwd~Length,x)))
  )
  
  user  system elapsed 
    25.80    0.06   26.02
  
  system.time(
    lres2 &lt;- I %&gt;% group_by(ERF, Wafer, Row, Col) %&gt;% do(
      as.data.frame.list(coef(lm(Rds.on.fwd~Length, data=.))))
    )
  
  user  system elapsed 
    43.12    0.25   44.02 
  
  system.time(
  lres3 &lt;- setDT(I)[, as.list(coef(lm(Rds.on.fwd~Length))), .(ERF,Wafer, Row, Col)]
  )
  
  user  system elapsed 
    19.77    0.05   19.91
  </code></pre>
  
  <p>so actually @akrun the data.table option is the best so thank you again. </p>