I used the following to assign each row the group average, where w is the new group average variable, x is the variabale to be averaged, and g is the nominal group indicator: w <- ave(x,g) Now I want to calculate the group average, but WITHOUT each row's value of x. Is there an easy way to do this? I'm sure I'm missing something obvious here, but for the life of me I can't figure it out. Thanks! -- Casey A. Klofstad University of Miami Department of Political Science Coral Gables, FL klofstad at gmail.com http://moya.bus.miami.edu/~cklofstad
do you mean something like this: tapply(x, g, function (y) { sapply(1:length(y), function (i) mean(y[-i])) }) I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Casey Klofstad" <klofstad at gmail.com> To: <r-help at r-project.org> Sent: Tuesday, November 13, 2007 4:45 PM Subject: [R] group mean, minus each row's value>I used the following to assign each row the group average, where w is > the new group average variable, x is the variabale to be averaged, > and > g is the nominal group indicator: > > w <- ave(x,g) > > Now I want to calculate the group average, but WITHOUT each row's > value of x. Is there an easy way to do this? I'm sure I'm missing > something obvious here, but for the life of me I can't figure it > out. > > Thanks! > -- > Casey A. Klofstad > University of Miami > Department of Political Science > Coral Gables, FL > > klofstad at gmail.com > http://moya.bus.miami.edu/~cklofstad > > ______________________________________________ > 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. >Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
Try:> w <- ave(x,g) > n <- ave(x,g, FUN=length) > wnew <- (w*n - x)/(n-1)Or> s <- ave(x,g, FUN=sum) > n <- ave(x,g, FUN=length) > wnew <- (s-x)/(n-1)Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Casey Klofstad > Sent: Tuesday, November 13, 2007 8:45 AM > To: r-help at r-project.org > Subject: [R] group mean, minus each row's value > > I used the following to assign each row the group average, > where w is the new group average variable, x is the variabale > to be averaged, and g is the nominal group indicator: > > w <- ave(x,g) > > Now I want to calculate the group average, but WITHOUT each > row's value of x. Is there an easy way to do this? I'm sure > I'm missing something obvious here, but for the life of me I > can't figure it out. > > Thanks! > -- > Casey A. Klofstad > University of Miami > Department of Political Science > Coral Gables, FL > > klofstad at gmail.com > http://moya.bus.miami.edu/~cklofstad > > ______________________________________________ > 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. >