I have a data frame that consists of the following columns (simplified): Date, ID, Price:
Date ID Price
1/2/2013 05947U4Q8 25
1/2/2013 05947UT40 9.40264
1/2/2013 07387BAW3 8.75
1/2/2013 07387BBJ1 4.4861
1/2/2013 07387BEQ2 5
1/2/2013 12513EAY0 6
1/2/2013 20047PAS6 33
1/3/2013 05947UT40 9.40414
1/3/2013 07387BAW3 8.75
1/3/2013 07387BBJ1 4.4742
1/3/2013 07387BEQ2 5
1/3/2013 12513EAY0 6
1/3/2013 20047PAS6 33
So, for every Date, there are several IDs, with a single Price for each. The IDs may change from one day to the next (some dropping off, others being added). What I'm trying to compute is, for every day, the change in price for every ID (if ID's price was known the previous day). So, for the example above, the output should be:
Date ID Price change
1/3/2013 05947UT40 0.0015
1/3/2013 07387BAW3 0
1/3/2013 07387BBJ1 -0.0119
1/3/2013 07387BEQ2 0
1/3/2013 12513EAY0 0
1/3/2013 20047PAS6 0
Naively using:
tapply(dataSet$Price, as.Date(dataSet$Date), diff)
does not work, i.e. does not give me what I'm looking for.