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)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? on a related point, is there a way to combine the two steps? 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 [[alternative HTML version deleted]]
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 johnsonDavid Winsemius, MD West Hartford, CT
I think the column is wrong in adf <- aggregate(.~mm[,1], data=mm, sum) Try 2 rather than 1 but this is just a guess since the sample data is not easily useable.. Please supply your sample data using dput(() See ?dput for information. It makes llife much easier for the reader. John Kane Kingston ON Canada> -----Original Message----- > From: mcooganj at gmail.com > Sent: Wed, 13 Jun 2012 05:32:16 +0200 > To: r-help at r-project.org > Subject: [R] templated use of aggregate > > 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) > > 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? > > on a related point, is there a way to combine the two steps? 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!