s-news
[Top] [All Lists]

Re: calculating the z scores by apply function

To: "John Kershaw" <kershaw@unb.ca>
Subject: Re: calculating the z scores by apply function
From: "Khan, Sohail" <khan@cshl.edu>
Date: Tue, 3 Apr 2007 11:55:07 -0400
Cc: <s-news@lists.biostat.wustl.edu>
In-reply-to: <Pine.GSO.4.58.0704021900200.2519@jupiter.sun.csd.unb.ca>
Thread-index: Acd1crwY4h5LSHL4SFq4EEP1Vt4M2gAlZ+Lg
Thread-topic: [S] calculating the z scores by apply function
Thanks JAK!
Awesome.....

-Sohail

-----Original Message-----
From: John Kershaw [mailto:kershaw@unb.ca]
Sent: Monday, April 02, 2007 6:03 PM
To: Khan, Sohail
Cc: s-news@lists.biostat.wustl.edu
Subject: Re: [S] calculating the z scores by apply function


Sohail,

This is relatively straightforward, apply outputs the results of the first
row as a column vector, then cbind() subsequent rows.  If you use
t(apply()) you should get the matrix you want:

temp<-matrix(c(rep(1,3),rep(2,3),rep(3,3)),nrow=3,byrow=T)
> temp
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3
> apply(temp,1,cumsum)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9
> t(apply(temp,1,cumsum))
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    2    4    6
[3,]    3    6    9

for example.

Hope that helps.

JAK

On Mon, 2 Apr 2007, Khan, Sohail wrote:

> Dear all,
>
> I have a large data.frame as follows:
> x1    x2      x3      x4      x5
> 2.56  3.02    4.00    3.87    5.25
> 1.25  2.20    1.75    2.32    2.14
> 6.02  5.98    4.99    5.32    5.66
>
> I would like to calculate the z scores for each row.  I am using the package 
> "outliers" which has a function for z cores.  I am using this function in 
> apply().  However, the out put is not a matrix with the same dim() as the 
> original input data.  For example, my data frame is 54675 X 15....and the 
> output is reversed 15 X 54675.  I cant figure this out.  I would appreciate 
> your expert advice.  Thanks.
>
> Sohail Khan
> Scientific Programmer
> COLD SPRING HARBOR LABORATORY
> Genome Research Center
> 500 Sunnyside Boulevard
> Woodbury, NY 11797
> (516)422-4076
>
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu.  To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message:  unsubscribe s-news
>
<Prev in Thread] Current Thread [Next in Thread>