> Message: 2
> Date: Mon, 10 Oct 2011 19:57:33 -0400
> From: "Robert A'gata" <rhelpacc@gmail.com>
> To: <r-sig-finance@stat.math.ethz.ch>
> Subject: [R-SIG-Finance] Aggregating time series by key and time
> Message-ID:
>        <CAA7=MyWPGMcgmC_ye8Dq3pfKko-OH6bGhzJAwase+vXJLgbeJA@mail.gmail.com
> >
> Content-Type: text/plain; charset="ISO-8859-1"
>
> Hi,
>
> I have a time series. Mostly trade time series which contain time,
> tradeType, tradePrice and tradeSize columns. Says
>
>                                     trdType trdPrice trdSize
> 2011-10-07 12:17:50       3 1357.998       9
> 2011-10-07 12:29:10       1 1351.480       4
> 2011-10-07 12:30:56       3 1361.366       8
> 2011-10-07 12:30:56       3 1361.366       3
> 2011-10-07 12:32:33       3 1316.613       4
> 2011-10-07 12:34:20       2 1321.174       4
> 2011-10-07 12:35:17       3 1345.164      10
> 2011-10-07 12:40:23       1 1341.528      10
> 2011-10-07 12:40:23       2 1331.528       4
> 2011-10-07 12:43:03       2 1332.578       8
>
> I'd like to aggregate trdSize for trades occurred at the same
> timestamp and of the same trade type. So it is not only aggregating by
> time. I'm wondering if there is a good way to do this. Any advice
> would be greatly appreciated. For reference, the output of such a
> function for the above example would be:
>
>                                     trdType trdPrice trdSize
> 2011-10-07 12:17:50       3 1357.998       9
> 2011-10-07 12:29:10       1 1351.480       4
> 2011-10-07 12:30:56       3 1361.366      11
> 2011-10-07 12:32:33       3 1316.613       4
> 2011-10-07 12:34:20       2 1321.174       4
> 2011-10-07 12:35:17       3 1345.164      10
> 2011-10-07 12:40:23       1 1341.528      10
> 2011-10-07 12:40:23       2 1331.528       4
> 2011-10-07 12:43:03       2 1332.578       8
>
> Because at 2011-10-07 12:30:56 we have 2 trades of trdType=3 occurred
> with sizes 8 and 3. However, at 2011-10-07 12:40:23, we have 2 trades
> occurred at the same price but they differ in trdType. So we do not
> perform aggregation. Thank you.
>
> Best regards,
>
> Robert
>
>
You can use the excellent package: ddply.

In your above example, assume you have the data in dataframe pv.

First turn the timestamp into a number representing seconds from the start
of the universe
pv$ts <- unclass(as.POSIXct(strptime(pv[,1], '%m/%d/%Y %H:%M:%S')))

And then use ddply:

ddply(pv[,-1], c("ts","trdType"), function(df) sum(df$trdSize))

	[[alternative HTML version deleted]]

