[R] templated use of aggregate
David Winsemius
dwinsemius at comcast.net
Wed Jun 13 15:06:45 CEST 2012
On Jun 12, 2012, at 11:32 PM, Matthew Johnson wrote:
> Dear R-help,
>
> I have an xts data set that i have subset by date.
>
> now it contains a date-time-stamp, and two columns (price and volume
> traded): my objective is to create tables of volume traded at a
> price - and
> i've been successfully using aggregate to do so in interactive use.
>
> say the data looks as follows:
>
> px_ym1 vol_ym1
> 2012-06-01 09:37:00 97.91 437
> 2012-06-01 09:37:00 97.91 64
> 2012-06-01 09:37:00 97.91 1
> 2012-06-01 09:37:00 97.91 5
> 2012-06-01 09:37:00 97.91 5
> 2012-06-01 09:37:00 97.92 174
> 2012-06-01 09:37:00 97.92 64
> 2012-06-01 09:37:00 97.92 125
> 2012-06-01 09:37:00 97.92 124
> 2012-06-01 09:37:00 97.92 64
> 2012-06-01 09:37:00 97.92 109
> 2012-06-01 09:37:00 97.92 64
> 2012-06-01 09:37:00 97.92 19
> 2012-06-01 09:37:00 97.92 45
> 2012-06-01 09:37:00 97.92 75
> 2012-06-01 09:37:00 97.92 3
> 2012-06-01 09:37:00 97.92 47
> 2012-06-01 09:37:00 97.91 26
> 2012-06-01 09:37:00 97.92 4
> 2012-06-01 09:37:00 97.92 1
>
> the the following gives me what i'm looking for:
>
>> adf <- aggregate(.~px_ym1, data=mm, sum)
>
> which is this table:
>
> px_ym1 vol_ym1
> 1 97.91 538
> 2 97.92 918
>
> however now i'm trying to code it to run automatically, and use of the
> templated version:
>
>> adf <- aggregate(.~mm[,1], data=mm, sum)
Did you try:
adf <- aggregate(mm[,-1] ~ mm[,1], data=mm, sum)
adf
I would have used names:
adf <- aggregate(vol_ym1 ~ px_ym1, data=mm, sum)
adf
> yields the following - which contains what i'd like, but is has also
> summed
> across the price column (not ideal).
>
> px_ym1 px_ym1 vol_ym1
> 1 97.91 587.46 538
> 2 97.92 1370.88 918
>
> how do i code this so that i can enter an xts data-frame with
> arbitrary
> names and still obtain the table with only the information i desire?
That is too far to the vague side of the vague-specific continuum.
>
> on a related point, is there a way to combine the two steps?
Er, which two steps would that be?
> the function
> i've written splits by date and then returns a list containing data-
> frames
> that report the volume traded at each price on each date
>
> - am i re-creating the wheel here? is there canned function that
> does this?
>
> thanks + best regards
>
> matt johnson
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list