Hi: d$r <- with(d, ave(x, f, FUN = function(u) u - mean(u))) > d f x r 1 1 2 1 2 1 0 -1 3 2 0 -2 4 2 4 2 5 2 2 0
HTH, Dennis On Thu, Nov 18, 2010 at 6:02 AM, Lancaster, Anthony < anthony_lancas...@brown.edu> wrote: > Hi, > I'd appreciate help with this. I have a data matrix with one column, called > f in the example below, a factor. I'd like to subtract the means from each > of > other columns for each level of the factor. That is, in the example, to go > from the first matrix below to the second. I know SWEEP will take out > means, > but I want to do this for each level of the factor. > f x > 1 2 > 1 0 > 2 0 > 2 4 > 2 2 > > f xnew > 1 1 > 1 -1 > 2 -2 > 2 2 > 2 0 > Thanks > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > [[alternative HTML version deleted]] ______________________________________________ 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.