[R] sweep by levels of a factor

2010-11-18 Thread Lancaster, Anthony
  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.
  fx
 1   2
 1   0
  2   0
  2   4
  2   2

 f   xnew
 11
 1   -1
 2   -2
 22
 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.


Re: [R] sweep by levels of a factor

2010-11-18 Thread Lancaster, Anthony
Thank you very much,

Tony L.

On 18 November 2010 14:02, Lancaster, Anthony
anthony_lancas...@brown.eduwrote:

   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.
   fx
  1   2
  1   0
   2   0
   2   4
   2   2

  f   xnew
  11
  1   -1
  2   -2
  22
  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.


Re: [R] sweep by levels of a factor

2010-11-19 Thread Lancaster, Anthony
Many thanks for your most helpful answers.

Tony Lancaster

On 18 November 2010 20:28, David Winsemius dwinsem...@comcast.net wrote:


 On Nov 18, 2010, at 1:23 PM, Henrique Dallazuanna wrote:

  Try this also:

 x$x - ave(x$x, x$f)


 Could also use scale for only its centering capacities:

 unlist(
   tapply(fx$x, fx$f, scale, scale=FALSE) # default for center == TRUE
   )
 11 12 21 22 23
  1 -1 -2  2  0

 ... but looking at the code for ave and for tapply makes me think ave would
 win in a foot race. scale() has other capacities that make its availability
 useful.

 --
 David.



 On Thu, Nov 18, 2010 at 12:02 PM, 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.
fx
   1   2
   1   0
2   0
2   4
2   2

   f   xnew
   11
   1   -1
   2   -2
   22
   2 0
 Thanks

  [[alternative HTML version deleted]]


 David Winsemius, MD
 West Hartford, CT



[[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.