s-news
[Top] [All Lists]

Re: Calculation done on rows for select columns

To: <Jason.Overstreet@honeywell.com>, <s-news@lists.biostat.wustl.edu>
Subject: Re: Calculation done on rows for select columns
From: <Bill.Venables@csiro.au>
Date: Tue, 24 Jan 2006 09:46:40 +1100
Domainkey-signature: s=email; d=csiro.au; c=nofws; q=dns; b=sQfu1kodk+Mhxb66Uu/ICjpRJbsVnIFUfGlw4/ukWXRUFKXHaGo3Q7F6n6JI4eKFvc/GHoyVfYyYZEPNpxMpFoplhqGGXmZKyiSMzYIRTZB79bFriDbzIuPgassjCcfH;
Thread-index: AcYgR5/jvxDGyBknSG2WuIr1VSLnlAAJbFgg
Thread-topic: [S] Calculation done on rows for select columns
I think step-by-step is about the only way (warning: untested code!):

x <- as.matrix(xy[, 1:3])
x <- cbind(x, mean.x = apply(x, 1, mean), var.x = apply(x, 1, var))
y <- as.matrix(xy[, 4:6])
y <- cbind(y, mean.y = apply(y, 1, mean), var.y = apply(y, 1, var))

xy <- data.frame(x, y)

If you have more than two pieces you can put this kind of thing into a
loop easily enough.  I don't think there's a 'zap! and its done'
solution, though, unless you write one.

Bill Venables.

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of Overstreet,
Jason (FL51)
Sent: Tuesday, 24 January 2006 3:56 AM
To: s-news@lists.biostat.wustl.edu
Subject: [S] Calculation done on rows for select columns


I have a data frame as shown below called xy:
> xy
  x.1 x.2 x.3 y.1 y.2 y.3 
1   1   5   9   2  10  18
2   2   6  10   4  12  20
3   3   7  11   6  14  22
4   4   8  12   8  16  24
>
I was wondering what would be an easy way to have the mean and variance
calculated and stored as follows:
>       xy

        x.1     x.2     x.3     mean.x  var.x   y.1     y.2     y.3
mean.y  var.y
1       1       5       9       5       16      2       10      18
10      64
2       2       6       10      6       16      4       12      20
12      64
3       3       7       11      7       16      6       14      22
14      64
4       4       8       12      8       16      8       16      24
16      64
>



Jason Overstreet
Statistician
Honeywell DSES
(727) 539-3964 

<Prev in Thread] Current Thread [Next in Thread>