[R] simple matrix question

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Jun 4 15:56:53 CEST 2003


Paul Lemmens <P.Lemmens at nici.kun.nl> writes:

> Hoi Christoph,
> 
> --On woensdag 4 juni 2003 15:21 +0200 Christoph Lehmann
> <lehmann at puk.unibe.ch> wrote:
> 
> > what is the easiest way to get from
> > x
> >      x1 x2
> > [1,]  2  3
> > [2,]  3  2
> > [3,]  1  3
> > [4,]  1  4
> >
> > xbar1
> >        x1 x2
> > [1,] 1.75  3
> > [2,] 1.75  3
> > [3,] 1.75  3
> > [4,] 1.75  3
> >
> > with the mean of the columns of x as values?
> >
> xbar1 <- tapply(as.vector(x), gl(2,4), mean);

No. I think you're missing the point (it gives a 2-vector rather than
a 4x2 matrix, and apply(x,2,mean) is quicker for that). Try

sweep(x,2,apply(x,2,mean),function(x,y)y)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list