title: How to rearrange output from dplyr
body: <p>When I write the following code </p>
  
  <pre><code>   ddply(milkers, .(dim_cat, lact_cat), function(x) mean(x$milkyield))
  </code></pre>
  
  <p>I get the following output</p>
  
  <p><img src="https://i.stack.imgur.com/OpYMW.png" alt="Preliminary output with 2 rows per time period"></p>
  
  <p>The mean calculations regarding milk production by class of stock (1 vs 2) are correct.  I would like to end up with a table more like the one below. </p>
  
  <p><img src="https://i.stack.imgur.com/43sz0.png" alt="Desired table format with one row per dim time period"></p>
  
  <p>Effectively I am trying to get the number of animals in each time period and calculate their mean milk production.  The problem is that it is calculating the total number of animals for all time periods and mean milk production for all time periods.  </p>
  
  <p>The code I used to generate this data is below.  </p>
  
  <pre><code>heiferdat &lt;- subset(milkers, lact_cat== 1)
  cowdat &lt;- subset(milkers, lact_cat== 2)
  
  
  ddply(milkers, .(dim_cat), function(x) c(Heifers = sum(milkers$lact_cat==1), H_Milk= mean(heiferdat$milkyield), Cows = sum(milkers$lact_cat==2), C_Milk= mean(cowdat$milkyield)))
  </code></pre>
  
  <p>I had anticipated that in this code the .(dim_cat) variable would be applied to the function to restrict the sum and mean functions to only include animals in the correct time period.  </p>
  
  <p>I am looking for advice as to how I can get the output with one row per time period with the number of animals for each class lact_cat and the mean milk production for each lact_cat</p>
  
  <p>Thank you</p>
  
  <p>The following is a subset of the data that i am working with.</p>
  
  <pre><code>dput(milkers[180:200, c(11, 25, 26)]) 
  dput(heiferdat[1:20, c(11, 25, 26)])
  dput(cowdat[1:20, c(11, 25, 26)])
  
  &gt; dput(milkers[180:200, c(11, 25, 26)]) 
  structure(list(milkyield = structure(c(8.42, 38.32, 14.27, 7.68, 
  16.59, 17.19, 24.45, 33.47, 36.16, 25.88, 11.61, 18.96, 11.27, 
  33.6, 21.57, 20.87, 9.62, 7.93, 21.02, 17.75, 22.01), label = "Milk (L)", class = c("labelled", 
  "numeric")), dim_cat = structure(c(5L, 3L, 7L, 7L, 2L, 7L, 2L, 
  2L, 2L, 3L, 6L, 6L, 2L, 3L, 6L, 6L, 6L, 6L, 6L, 7L, 6L), .Label = c("&lt;31", 
  "31-90", "91-150", "151-210", "211-270", "271-330", "&gt;330"), class = c("labelled", 
  "factor"), label = "Days in Milk"), lact_cat = structure(c(2L, 
  2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L), .Label = c("1", "2"), class = "factor")), row.names = 180:200, class = "data.frame")
  
  &gt; dput(heiferdat[1:20, c(11, 25, 26)]) 
  structure(list(milkyield = structure(c(14.27, 17.19, 11.61, 18.96, 
  11.27, 21.57, 20.87, 9.62, 7.93, 21.02, 17.75, 22.01, 25.15, 
  11.75, 12.6, 15.62, 19.29, 8.85, 15.52, 11.62), label = "Milk (L)", class = c("labelled", 
  "numeric")), dim_cat = structure(c(7L, 7L, 6L, 6L, 2L, 6L, 6L, 
  6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L), .Label = c("&lt;31", 
  "31-90", "91-150", "151-210", "211-270", "271-330", "&gt;330"), class = c("labelled", 
  "factor"), label = "Days in Milk"), lact_cat = structure(c(1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L), .Label = c("1", "2"), class = "factor")), row.names = c(182L, 
  185L, 190L, 191L, 192L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 
  201L, 202L, 203L, 204L, 205L, 206L, 207L, 208L), class = "data.frame")
  
  &gt; dput(cowdat[1:20, c(11, 25, 26)]) 
  structure(list(milkyield = structure(c(15.73, 14.56, 16.94, 16.25, 
  39.09, 9.79, 8.41, 3.05, 38.89, 11.7, 29.89, 19.73, 18.2, 20.63, 
  20.32, 52.99, 10.11, 8.08, 10.84, 33.75), label = "Milk (L)", class = c("labelled", 
  "numeric")), dim_cat = structure(c(3L, 6L, 6L, 2L, 3L, 7L, 6L, 
  7L, 3L, 7L, 3L, 6L, 3L, 6L, 2L, 2L, 7L, 6L, 7L, 7L), .Label = c("&lt;31", 
  "31-90", "91-150", "151-210", "211-270", "271-330", "&gt;330"), class = c("labelled", 
  "factor"), label = "Days in Milk"), lact_cat = structure(c(2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L), .Label = c("1", "2"), class = "factor")), row.names = c(NA, 
  20L), class = "data.frame")
  </code></pre>