Below are the two tables
Table1
Date OldPrice NewPrice
2014-06-12 09:32:56 0 10
2014-06-27 16:13:36 10 12
2014-08-12 22:41:47 12 13
Table2
Date Qty
2014-06-15 18:09:23 5
2014-06-19 12:04:29 4
2014-06-22 13:21:34 3
2014-06-29 19:01:22 6
2014-07-01 18:02:33 3
2014-09-29 22:41:47 6
I want to display the result in this manner
Date OldPrice NewPrice Qty
2014-06-12 09:32:56 0 10 0
2014-06-27 16:13:36 10 12 12
2014-08-12 22:41:47 12 13 15
I used the command
for(i in 1:nrow(Table1)){
startDate = Table1$Date[i]
endDate = Table1$Date[i+1]
code=aggregate(list(Table2$Qty),
by=list(Table1$Date, Table1$OldPrice, Table1$NewPrice, Date = Table2$Date > startDate & Table2$Date <= endDate), FUN=sum)
}
I want the quantity to be aggregated between the given dates in first table, i.e between the first and second dates, second and third dates and so on. Also the last date needs to be aggregated till the end of date in the other table.
Thanks in advance!
I see what you mean in this question as compared to your previous one
This gives you the exact output you provide:
produced by the following code (see the explanation):
I think you got interesting problems to solve just a bit unfortunate way of communicating it to the SO community. For instance, it was not clear what to do with the dates in df2 that come before any available range in df1, hence in the code above, I add these quantities (if there are any) to the very first date. This is more generic and analogous to what you expected from the dates in df2 that come after ranges of dates in df1 (add them to the last date of df1).