s-news
[Top] [All Lists]

Re: Calculation done on rows for select columns

To: "Overstreet, Jason" <Jason.Overstreet@honeywell.com>
Subject: Re: Calculation done on rows for select columns
From: Tim Hesterberg <timh@insightful.com>
Date: 23 Jan 2006 10:22:16 -0800
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <04A1697DA10CC54EAC443DD63568766E69F4A8@fl51exch99.space.honeywell.com> (Jason.Overstreet@honeywell.com)
References: <04A1697DA10CC54EAC443DD63568766E69F4A8@fl51exch99.space.honeywell.com>
You could use a combination of rowMeans, rowVars, and 
data[ grep( "^x", names(data)) ]

datax = data[ grep( "^x", names(data)) ]
datay = data[ grep( "^y", names(data)) ]
data.frame(datax, mean.x = rowMeans(datax), var.x =rowVars(datax),
           datay, mean.y = rowMeans(datay), var.y =rowVars(datay))

Tim Hesterberg

>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


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