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]]
Hi: d$r <- with(d, ave(x, f, FUN = function(u) u - mean(u)))> df 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_lancaster@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]]
Thank you very much, Tony L. On 18 November 2010 14:02, Lancaster, Anthony <anthony_lancaster@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]]
Try this also: x$x - ave(x$x, x$f) On Thu, Nov 18, 2010 at 12:02 PM, Lancaster, Anthony < anthony_lancaster@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]