I'm interested in finding out how to sum a column based on the seconds value of a time column in a data.table
For example, let's say I have a data table as follows:
Time | Inventory
----------------------------------------------
08-01-2001 11:50:12 | 5
08-01-2001 11:50:16 | 8
08-01-2001 11:50:17 | 2
08-01-2001 11:50:17 | 1
08-01-2001 11:50:19 | 10
08-01-2001 11:50:23 | 5
08-01-2001 11:50:23 | 9
08-01-2001 11:51:23 | 12
Then I would like to be able to be able to sum the Inventory
based on the seconds value in the Time
column such that I get a data.table as follows:
Time | Inventory
----------------------------------------------
08-01-2001 11:50:12 | 5
08-01-2001 11:50:16 | 8
08-01-2001 11:50:17 | 3
08-01-2001 11:50:17 | 3
08-01-2001 11:50:19 | 10
08-01-2001 11:50:23 | 14
08-01-2001 11:50:23 | 14
08-01-2001 11:51:23 | 12
I've tried using variations of the Aggregate()
function but these always seem to delete rows which are repeated, and I do not wish to do this. Is there a way I could perform this action using data.table
or perhaps xts
? Thanks in advance.
EDIT: Here is the dput
output:
structure(list(Timecol = c("0008-01-20 00:00:00", "0008-01-20 00:00:00",
"0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00",
"0008-01-20 00:00:00", "0008-01-20 00:00:00", "0008-01-20 00:00:00"
), ID = c("11", "11", "11", "11", "11", "11", "11", "11"), Inventorycol = c("5",
"8", "2", "1", "10", "5", "9", "12")), .Names = c("Timecol",
"ID", "Inventorycol"), row.names = c(NA, -8L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x00000000028b0788>)