I have following data for which I need to calculate the weighted aggregate value of the parameter at each time. Date,Parameter,Weight 2012-01-31,90,200 2012-01-31,80,400 2012-01-31,70,500 2012-01-31,60,800 2012-02-29,120,220 2012-02-29,110,410 2012-02-29,75,520 2012-02-29,65,840 2012-03-31,115,210 2012-03-31,100,405 2012-03-31,70,500 2012-03-31,60,800 So for the above sample the solution looks like: Date,Weighted Parameter 2012-01-31,70 2012-02-29,82.96482412 2012-03-31,77.10182768 Could I potentially use tapply / aggregate for this? Would like to avoid a for loop if possible. Thank you! ________________________________ The information transmitted is intended solely for the individual or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you have received this email in error please contact the sender and delete the material from any computer. Any information contained herein is neither an offer to sell nor a solicitation to buy any interest in any investment fund. An offer can only be made by the approved offering memorandum, which contains important information concerning risk factors and other material information and must be read carefully before any decision to invest is made. Securities and derivatives trading are speculative and involve a risk of substantial loss. Past results are not necessarily indicative of future performance. All incoming and outgoing e-mails are archived and may be reviewed and/or produced at the request of regulators or in connection with civil litigation. IRON Holdings, LLC. accepts no liability for any errors or omissions arising as a result of transmission. [[alternative HTML version deleted]]
On May 17, 2013, at 11:48 AM, Chirag Maru wrote:> I have following data for which I need to calculate the weighted aggregate value of the parameter at each time. > > Date,Parameter,Weight > 2012-01-31,90,200 > 2012-01-31,80,400 > 2012-01-31,70,500 > 2012-01-31,60,800 > 2012-02-29,120,220 > 2012-02-29,110,410 > 2012-02-29,75,520 > 2012-02-29,65,840 > 2012-03-31,115,210 > 2012-03-31,100,405 > 2012-03-31,70,500 > 2012-03-31,60,800 > > So for the above sample the solution looks like: > > Date,Weighted Parameter > 2012-01-31,70 > 2012-02-29,82.96482412 > 2012-03-31,77.10182768 > > Could I potentially use tapply / aggregate for this? Would like to avoid a for loop if possible.> by(dat, dat[1], FUN=function(d) weighted.mean(d[["Parameter"]], w=d[["Weight"]]) )Date: 2012-01-31 [1] 70 ------------------------------------------------------------ Date: 2012-02-29 [1] 82.96482 ------------------------------------------------------------ Date: 2012-03-31 [1] 77.10183 It's a bit of a shame that there is no as.data.frame.by function. You can create a dataframe from the by object with as.data.frame.table with the only defect in the naming of the second column as.data.frame.table(by(dat, dat[1], FUN=function(d) weighted.mean(d[["Parameter"]], w=d[["Weight"]]) )) Date Freq 1 2012-01-31 70.00000 2 2012-02-29 82.96482 3 2012-03-31 77.10183 setNames(as.data.frame.table(by(dat, dat[1], FUN=function(d) weighted.mean(d[["Parameter"]], w=d[["Weight"]]) )), c("Dts", "wtdmeans")) Dts wtdmeans 1 2012-01-31 70.00000 2 2012-02-29 82.96482 3 2012-03-31 77.10183 -- David Winsemius Alameda, CA, USA
Hi, May be this helps: dat<- read.table(text=" Date,Parameter,Weight 2012-01-31,90,200 2012-01-31,80,400 2012-01-31,70,500 2012-01-31,60,800 2012-02-29,120,220 2012-02-29,110,410 2012-02-29,75,520 2012-02-29,65,840 2012-03-31,115,210 2012-03-31,100,405 2012-03-31,70,500 2012-03-31,60,800 ",sep=",",header=TRUE,stringsAsFactors=FALSE) library(plyr) ?ddply(dat,.(Date), summarize, wtdmeans=weighted.mean(Parameter,Weight)) #??????? Date wtdmeans #1 2012-01-31 70.00000 #2 2012-02-29 82.96482 #3 2012-03-31 77.10183 A.K. ----- Original Message ----- From: Chirag Maru <chirag.maru at ironfinancial.com> To: "r-help at R-project.org" <r-help at r-project.org> Cc: Sent: Friday, May 17, 2013 2:48 PM Subject: [R] time-series aggregation of information I have following data for which I need to calculate the weighted aggregate value of the parameter at each time. Date,Parameter,Weight 2012-01-31,90,200 2012-01-31,80,400 2012-01-31,70,500 2012-01-31,60,800 2012-02-29,120,220 2012-02-29,110,410 2012-02-29,75,520 2012-02-29,65,840 2012-03-31,115,210 2012-03-31,100,405 2012-03-31,70,500 2012-03-31,60,800 So for the above sample the solution looks like: Date,Weighted Parameter 2012-01-31,70 2012-02-29,82.96482412 2012-03-31,77.10182768 Could I potentially use tapply / aggregate for this?? Would like to avoid a for loop if possible. Thank you! ________________________________ The information transmitted is intended solely for the individual or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of or taking action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you have received this email in error please contact the sender and delete the material from any computer. Any information contained herein is neither an offer to sell nor a solicitation to buy any interest in any investment fund. An offer can only be made by the approved offering memorandum, which contains important information concerning risk factors and other material information and must be read carefully before any decision to invest is made. Securities and derivatives trading are speculative and involve a risk of substantial loss. Past results are not necessarily indicative of future performance. All incoming and outgoing e-mails are archived and may be reviewed and/or produced at the request of regulators or in connection with civil litigation. IRON Holdings, LLC. accepts no liability for any errors or omissions arising as a result of transmission. ??? [[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.