s-news
[Top] [All Lists]

Re: calculating the z scores by apply function

To: "Khan, Sohail" <khan@cshl.edu>
Subject: Re: calculating the z scores by apply function
From: John Kershaw <kershaw@unb.ca>
Date: Mon, 2 Apr 2007 19:03:17 -0300 (ADT)
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <C8696843AE995F4EA4CDC3E2B83482A9018791E7@mailbox02.cshl.edu>
References: <C8696843AE995F4EA4CDC3E2B83482A9018791E7@mailbox02.cshl.edu>
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>