Skip to content
Back to formatted view

Raw Message

Message-ID: <x2y90iym4o.fsf@biostat.ku.dk>
Date: 2003-06-04T13:56:53Z
From: Peter Dalgaard
Subject: simple matrix question
In-Reply-To: <26170609.1054740497@lemmens.socsci.kun.nl>

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