[R] How to get standard deviation of rows in a matrix

Tobias Verbeke tobias.verbeke at telenet.be
Wed Mar 9 21:20:42 CET 2005


On Wed, 9 Mar 2005 13:49:44 -0600
"Jagarlamudi, Choudary" <choudary.jagar at swosu.edu> wrote:

> Hi all,
>  
>    I am trying to find sd of my rows in a matrix and i get column sd inspite of extracting rows.
> I tried to do the sqrt(var(x)) but that did'nt work as well,
>  
> Here is my data
>  
> genes
> 15 24 63 40
> 25 42 46 35
> 23 53 37 45
> 30 37 50 55
> 40 51 30 48
>  
> x<-sd(genes[1:5,])
>  
> y<-sqrt(var(genes[1:5,]))
>  
> I get 4 sds for the 4 columns instead of 5 sds for my 5 rows.
> Thanks you in advance.

> mymat <- matrix(rnorm(20), 5)
> mymat
           [,1]       [,2]       [,3]       [,4]
[1,] -0.1418666 -0.6754704 -0.2525154 -1.4832003
[2,] -0.2254920  1.8705093 -0.9678318 -0.1108883
[3,]  2.2501392  0.1687349 -0.1279790  0.7055311
[4,]  0.9893453 -0.5924199  0.2410576  0.9001638
[5,] -0.4179559  0.9334556 -0.6501605  0.6148958
> apply(mymat, 1, sd)
[1] 0.6084171 1.2135998 1.0584750 0.7318231 0.7722641

See ?apply

HTH,
Tobias




More information about the R-help mailing list