[R] Data frame aggregation

peter dalgaard pdalgd at gmail.com
Fri Oct 7 18:06:05 CEST 2011


On Oct 7, 2011, at 14:53 , PHILIP GLADWIN, BLOOMBERG TRADEBOOK: wrote:

> Hello,
> Could anybody help me with this question?
> 
> Example data frame
> NAME     TICKER     SHARES     PERFORMANCE
> John      ABC        100        0.05       
> John      ABC        1000       1.5        
> Alice     EFG        20         0.3       
> Paul      HIJ        50         1.0
> Paul      JKL        60         2.0
> Paul      MNO        12         3.0
> 
> 
> I would like to aggregate this dataframe by NAME, TICKER, and SHARES and to 
> perform a weighted mean.  I expect the output to be
> NAME   TICKER     SHARES       PERFORMANCE
> John   ABC        1000+100     ( (100 * 0.05) + (1000 * 1.5) ) / (100 + 1000)
> Alice  EFG        20           0.3
> Paul   HIJ        50           1.0
> Paul   JKL        60           2.0
> Paul   MNO        12           3.0
> 
> and I would like the output to be in a data frame.
> 
> Can anybody help?


You can do it in steps:

1. calculate a new variable, say TMP <- SHARES*PERFORMANCE
2. aggregate SHARES and TMP using sum()
3. in the aggregated data compute PERFORMANCE <- TMP/SHARES

(Interesting to see that we have users inside Bloomberg, give the recent trademark complaint against the rbloomberg package...)

-pd


> Thank you,
> Phil,
> 
> -------------------------------------------------------------------------------
> This e-mail is communicated by Bloomberg Tradebook Europe Limited a company registered in England & Wales No. 3556095 whose registered office is at City Gate House, 39-45 Finsbury Square, London EC2A 1 PQ and which is authorized and regulated by the UK Financial Services Authority registered under number 187492.  Bloomberg Tradebook Europe's services are only available to professional investors. This e-mail is not an offer or a solicitation to buy or sell, and does not constitute any investment advice or recommendation, of any security or other financial instrument.
> ______________________________________________
> 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.

-- 
Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list