It is not at all clear what you are doing. You state that the data set you are using is what I have called dat1 : see dput form below.
As far as I can see there is no numerical value in there. ##===========data set in dput form================# dat1 <- structure(list(Name = c("N1", "N1", "N1", "N1", "N1", "N1", "N1", "N2", "N2", "N2", "N2", "N2", "N2", "N2"), Ticker = c("T1", "T1", "T1", "T1", "T1", "T1", "T1", "T2", "T2", "T2", "T2", "T2", "T2", "T2"), Sector = c("S1", "S1", "S1", "S1", "S1", "S1", "S1", "S2", "S2", "S2", "S2", "S2", "S2", "S2"), Industry = c("I1", "I1", "I1", "I1", "I1", "I1", "I1", "I2", "I2", "I2", "I2", "I2", "I2", "I2"), Country = c("C1", "C1", "C1", "C1", "C1", "C1", "C1", "C2", "C2", "C2", "C2", "C2", "C2", "C2"), Year = c("FY-4", "FY-3", "FY-2", "FY-1", "FY0", "FY1", "FY2", "FY-4", "FY-3", "FY-2", "FY-2", "FY0", "FY2", "FY2"), ROE = c("ROE11", "ROE12", "ROE13", "ROE14", "ROE15", "ROE16", "ROE17", "ROE21", "ROE22", "ROE23", "ROE24", "ROE25", "ROE26", "ROE27"), EPS = c("EPS11", "EPS12", "EPS13", "EPS14", "EPS15", "EPS16", "EPS17", "EPS21", "EPS22", "EPS23", "EPS24", "EPS25", "EPS26", "EPS27"), MKTCAP = c("MKT11", "MKT12", "MKT13", "MKT14", "MKT15", "MKT16", "MKT17", "MKT21", "MKT22", "MKT23", "MKT24", "MKT25", "MKT26", "MKT27")), .Names = c("Name", "Ticker", "Sector", "Industry", "Country", "Year", "ROE", "EPS", "MKTCAP"), class = "data.frame", row.names = c(NA, -14L)) ## =================end of dataset==================# There is no FISCALYEAR variable that you specifed below > ddply (dataread , .(Sector, FISCALYEAR), summarise, > WROE=wavg(ROE, MKTCAP))) I think we need a bit more information. John Kane Kingston ON Canada > -----Original Message----- > From: anandpu...@gmail.com > Sent: Fri, 1 Mar 2013 11:01:42 -0500 > To: r-help@r-project.org > Subject: [R] Conditional Weighted Average (ddply or any other function) > > Hello R community, > > I am computing weighted average statistic by using ddply function: > > My data set is: > N1 T1 S1 I1 C1 FY-4 ROE11 EPS11 MKT11 > N1 T1 S1 I1 C1 FY-3 ROE12 EPS12 MKT12 > N1 T1 S1 I1 C1 FY-2 ROE13 EPS13 MKT13 > N1 T1 S1 I1 C1 FY-1 ROE14 EPS14 MKT14 > N1 T1 S1 I1 C1 FY0 ROE15 EPS15 MKT15 > N1 T1 S1 I1 C1 FY1 ROE16 EPS16 MKT16 > N1 T1 S1 I1 C1 FY2 ROE17 EPS17 MKT17 > N2 T2 S2 I2 C2 FY-4 ROE21 EPS21 MKT21 > N2 T2 S2 I2 C2 FY-3 ROE22 EPS22 MKT22 > N2 T2 S2 I2 C2 FY-2 ROE23 EPS23 MKT23 > N2 T2 S2 I2 C2 FY-2 ROE24 EPS24 MKT24 > N2 T2 S2 I2 C2 FY0 ROE25 EPS25 MKT25 > N2 T2 S2 I2 C2 FY2 ROE26 EPS26 MKT26 > N2 T2 S2 I2 C2 FY2 ROE27 EPS27 MKT27 > > with colnames: > (Name,Ticker,Sector,Industry,Country,Year,ROE,EPS,MKTCAP) > > I want to compute > 1) Weighted ROE based on Sector and Fiscal Year. > For firm N1 of Sector S1 and Fiscalyear FY-3 weight is > MKT1 / SUM(MKT, where Sector = S1, Fiscalyear FY-3) > > 2) Weighted ROE based on Country and Fiscal Year. > For firm N1 of Country C1 and Fiscalyear FY-3 weight is > MKT1 / SUM(MKT, where Country = C1, Fiscalyear FY-3) > > 3) Weighted ROE based on Country, Sector and Fiscal Year. > For firm N1 of Country C1, Sector S1 and Fiscalyear FY-3 > weight is MKT1 / SUM(MKT, where Country = C1, Sector = S1, Fiscalyear > FY-3) > > 4) Weighted ROE based on Country, Industry and Fiscal Year. > For firm N1 of Country C1, Industry I1 and Fiscalyear FY-3 > weight is MKT1 / SUM(MKT, where Country = C1, Industry = I1, Fiscalyear > FY-3) > > > I tried using ddply function: > ddply (dataread , .(Sector, FISCALYEAR), summarise, WROE=wavg(ROE, > MKTCAP))) > > where wavg <- function(x, wt) x %*% wt/sum(wt) > but this doesn't give me the right answer. > > I could try subseting the data into different sectors and compute the > weighted average which doesn't look like an elegant solution and would > defeat the purpose of ddply > > I coudn't think of properly using melt and cast functions to solve > this issue. Any help will be highly appreciated. > > Thanks and Regards, > Punit > > ______________________________________________ > R-help@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. ____________________________________________________________ GET FREE SMILEYS FOR YOUR IM & EMAIL - Learn more at http://www.inbox.com/smileys Works with AIM®, MSN® Messenger, Yahoo!® Messenger, ICQ®, Google Talk™ and most webmails ______________________________________________ R-help@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.