title: Grouping column entries by unique values in R
body: <p>I have a date frame ordered by a datetime column, that includes multiple rows for a single datetime stamp. I would like to condense each individual timestamp into a single row. The data frame contains a number of columns of data, some of which only change when the timestamp changes, however, there are other columns that have unique values on each row, even within a unique timestamp (c1a-c2b in example). For these columns, I would like to end up with a summed value in my dataset for all of the rows that have been condensed into one (note: the number of rows for each unique datetime stamp varies). </p>
  
  <p>Example data:</p>
  
  <pre><code>Data &lt;- structure(list(datetime = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 1L), .Label = c("01/04/2011 00:13", "31/03/2011 23:14"
  ), class = "factor"), dist = c(210L, 210L, 210L, 210L, 210L, 
  210L, 210L, 210L, 210L, 210L, 210L, 210L, 210L, 210L, 210L, 210L, 
  210L, 210L, 210L, 210L, 215L, 215L, 215L, 215L, 215L, 215L, 215L, 
  215L, 215L, 215L, 215L, 215L, 215L, 215L, 215L, 215L, 215L, 215L, 
  215L, 215L, 215L, 215L, 215L), n = c(8L, 8L, 8L, 8L, 8L, 8L, 
  8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 5L, 5L, 
  5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
  5L, 5L, 5L, 5L, 5L), c1a = c(184L, 184L, 200L, 200L, 200L, 220L, 
  220L, 220L, 220L, 220L, 220L, 220L, 220L, 220L, 220L, 200L, 200L, 
  200L, 200L, 200L, 200L, 200L, 200L, 200L, 100L, 100L, 100L, 100L, 
  100L, 100L, 100L, 100L, 100L, 100L, 70L, 70L, 70L, 70L, 70L, 
  70L, 70L, 70L, 70L), c1b = c(18.4, 18.4, 20, 20, 20, 22, 22, 
  22, 22, 22, 22, 22, 22, 22, 22, 20, 20, 20, 20, 20, 20, 20, 20, 
  20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 7, 7, 7, 7, 7, 7, 
  7, 7, 7), c2a = c(552L, 552L, 600L, 600L, 600L, 660L, 660L, 660L, 
  660L, 660L, 660L, 660L, 660L, 660L, 660L, 600L, 600L, 600L, 600L, 
  600L, 600L, 600L, 600L, 600L, 300L, 300L, 300L, 300L, 300L, 300L, 
  300L, 300L, 300L, 300L, 210L, 210L, 210L, 210L, 210L, 210L, 210L, 
  210L, 210L), c2b = c(55.2, 55.2, 60, 60, 60, 66, 66, 66, 66, 
  66, 66, 66, 66, 66, 66, 60, 60, 60, 60, 60, 60, 60, 60, 60, 30, 
  30, 30, 30, 30, 30, 30, 30, 30, 30, 21, 21, 21, 21, 21, 21, 21, 
  21, 21)), .Names = c("datetime", "dist", "n", "c1a", "c1b", "c2a", 
  "c2b"), class = "data.frame", row.names = c(NA, -43L))
  </code></pre>
  
  <p>Reads in like so:</p>
  
  <pre><code>datetime            dist    n   c1a c1b     c2a c2b
  31/03/2011 23:14    210     8   184 18.4    552 55.2
  31/03/2011 23:14    210     8   184 18.4    552 55.2
  31/03/2011 23:14    210     8   200 20      600 60       etc...
  </code></pre>
  
  <p>In my output data frame, I would also like to end up with a new column that lists the number of rows that there were for each unique datetime stamp in the original date frame. </p>
  
  <p>Example of what I would like to end up with:</p>
  
  <pre><code> dt1               dist  n  c1a     c1b     c2a     c2b     row_sum
   31/03/2011 23:14   210  8  4168    416.8   12504   1250.4  20
   01/04/2011 00:13   215  5  2430    243     7290    729     23
  </code></pre>
  
  <p>I’ve looked at functions like <code>to.period</code> but they do not do exactly what I want. I would greatly appreciate any suggestions. Thanks. </p>