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

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Thu 10 Mar 2005 - 07:46:13 EST

On Wed, 9 Mar 2005 13:49:44 -0600, "Jagarlamudi, Choudary" <choudary.jagar@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,])

This doesn't extract the rows, it just returns the same matrix. As the man page for sd says, when applied to a matrix it calculates the standard deviation of each column.

What you want to do is to use the apply function. Rows are the first dimension, so you would use

apply(genes, 1, sd)

Duncan Murdoch



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Mar 10 09:47:55 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:42 EST