title: dplyr summarise when function return is vector-valued?
body: <p>The <code>dplyr::summarize()</code> function can apply arbitrary functions over the data, but it seems that function must return a scalar value.  I'm curious if there is a reasonable way to handle functions that return a vector value without making multiple calls to the function.  </p>
  
  <p>Here's a somewhat silly minimal example. Consider a function that gives multiple values, such as:</p>
  
  <pre><code>f &lt;- function(x,y){
    coef(lm(x ~ y, data.frame(x=x,y=y)))
  }
  </code></pre>
  
  <p>and data that looks like:  </p>
  
  <pre><code>df &lt;- data.frame(group=c('A','A','A','A','B','B','B','B','C','C','C','C'), x=rnorm(12,1,1), y=rnorm(12,1,1))
  </code></pre>
  
  <p>I'd like to do something like:</p>
  
  <pre><code>df %&gt;% 
  group_by(group) %&gt;%
  summarise(f(x,y))
  </code></pre>
  
  <p>and get back a table that has 2 columns added for each of the returned values instead of the usual 1 column.  Instead, this errors with: <code>Expecting single value</code></p>
  
  <p>Of course we can get multiple values from <code>dlpyr::summarise()</code> by giving the function argument multiple times:</p>
  
  <pre><code>f1 &lt;- function(x,y) coef(lm(x ~ y, data.frame(x=x,y=y)))[[1]]
  f2 &lt;- function(x,y) coef(lm(x ~ y, data.frame(x=x,y=y)))[[2]]
  
  df %&gt;% 
  group_by(group) %&gt;%
  summarise(a = f1(x,y), b = f2(x,y))
  </code></pre>
  
  <p>This gives the desired output:</p>
  
  <pre><code>  group         a            b
  1     A 1.7957245 -0.339992915
  2     B 0.5283379 -0.004325209
  3     C 1.0797647 -0.074393457
  </code></pre>
  
  <p>but coding in this way is ridiculously crude and ugly.  </p>
  
  <p><code>data.table</code> handles this case more succinctly: </p>
  
  <pre><code>dt &lt;- as.data.table(df)
  dt[, f(x,y), by="group"]
  </code></pre>
  
  <p>but creates an output that extend the table using additional rows instead of additional columns, resulting in an output that is both confusing and harder to work with:</p>
  
  <pre><code> group           V1
  1:     A  1.795724536
  2:     A -0.339992915
  3:     B  0.528337890
  4:     B -0.004325209
  5:     C  1.079764710
  6:     C -0.074393457
  </code></pre>
  
  <p>Of course there are more classic <code>apply</code> strategies we could use here, </p>
  
  <pre><code>sapply(levels(df$group), function(x) coef(lm(x~y, df[df$group == x, ])))
  
  
                       A            B           C
  (Intercept)  1.7957245  0.528337890  1.07976471
  y           -0.3399929 -0.004325209 -0.07439346
  </code></pre>
  
  <p>but this sacrifices both the elegance and I suspect the speed of the grouping.  In particular, note that we cannot use our pre-defined function <code>f</code> in this case, but have to hard code the grouping into the function definition.</p>
  
  <p>Is there a <code>dplyr</code> function for handling this case?  If not, is there a more elegant way to handle this process of evaluating vector-valued functions over a data.frame by group?</p>