Jagarlamudi, Choudary
2005-Mar-09 19:49 UTC
[R] How to get standard deviation of rows in a matrix
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. Choudary Jagarlamudi Instructor Southwestern Oklahoma State University STF 254 100 campus Drive Weatherford OK 73096 Tel 580-774-7136 ________________________________
Sundar Dorai-Raj
2005-Mar-09 20:00 UTC
[R] How to get standard deviation of rows in a matrix
Jagarlamudi, Choudary wrote on 3/9/2005 1:49 PM:> 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. >This has come up several times in the past and a quick search of the archives would have lead you to: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/34169.html HTH, --sundar
Tobias Verbeke
2005-Mar-09 20:20 UTC
[R] How to get standard deviation of rows in a matrix
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
You should read ?var and ?sd more carefully. For a data frame or a matrix, var() returns the covariance matrix of the columns, whereas sd() returns the standard deviations of the columns. If you want standard deviations of the rows, you need to transpose the data. Andy> From: Jagarlamudi, Choudary > > 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. > > Choudary Jagarlamudi > Instructor > Southwestern Oklahoma State University > STF 254 > 100 campus Drive > Weatherford OK 73096 > Tel 580-774-7136 > > ________________________________ > >
Duncan Murdoch
2005-Mar-09 20:46 UTC
[R] How to get standard deviation of rows in a matrix
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,])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