0

I'm working with a data frame and it is formatted as follows:

Instrument |    Date    |    Time    | MilliSecond |  Volume
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:30:20  |    205      |   123
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:30:20  |    205      |   456
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:30:20  |    205      |   789
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:31:12  |    329      |   150
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:31:12  |    329      |   430
--------------------------------------------------------------
DEF        | 5/20/2012  |  10:32:40  |    430      |   58
--------------------------------------------------------------
DEF        | 5/20/2012  |  10:32:40  |    430      |   164
--------------------------------------------------------------
GHI        | 5/20/2012  |  10:33:27  |    531      |   762
--------------------------------------------------------------

What I'd like to do is sum the values in the Volume column based on the Instrument, the Date, the Time, and the Millisecond columns so that I am able to get a data frame that looks like this:

Instrument |    Date    |    Time    | MilliSecond |  Volume
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:30:20  |    205      |   1368
--------------------------------------------------------------
ABC        | 5/20/2012  |  10:31:12  |    329      |   580
--------------------------------------------------------------
DEF        | 5/20/2012  |  10:32:40  |    430      |   222
--------------------------------------------------------------
GHI        | 5/20/2012  |  10:33:27  |    531      |   762
--------------------------------------------------------------

(To put it simply, if the Instrument, Date, Time, and MilliSecond column are the same for several rows, I would like to sum the Volume and then put it all into one row)

Does anyone know a good way I can go about doing this? There was a similar question here before but it was a bit different and I can't really apply it to my data frame (especially since I have to sum the Volume based on several columns). Thanks in advance.

ThePlowKing
  • 341
  • 1
  • 4
  • 15
  • Almost all the answers in the dupe generalize to multiple columns for the grouping. Some of them even demonstrate it in the answer (docendo discimus's answer, for example). – Gregor Thomas Feb 03 '17 at 22:59
  • If you are unable to adapt those answer to meet yours, please show the attempt so that we can help you correct what you missed. Also, in the future please share data in a more easy to reproduce way, for example with `dput()`. [See here for more tips on making reproducible examples in R](http://stackoverflow.com/q/5963269/903061). – Gregor Thomas Feb 03 '17 at 23:04
  • Since all of your columns are redundant, you can just `aggregate` volume on one of them. I chose ms, but you could have picked any of them, since they all change at the same rate. `aggregate(mydata["Volume"], by=list(mydata$Milliseconds), "sum")` Where mydata is the name of the dataframe holding your original data. – Patrick Williams Feb 03 '17 at 23:10
  • Ah, I'm extremely sorry! I realize why none of the previous answers that have been mentioned before weren't working - I forgot that I had previously changed my data frame to a slightly different format (by using a package) which prevented functions from working on it properly. I've managed to solve the issue, should I delete this question since it's a duplicate? – ThePlowKing Feb 03 '17 at 23:22

0 Answers0