Skip to content

Standard Deviation of a matrix

6 messages · Paul Hiemstra, PIKAL Petr, David Winsemius +2 more

#
Hello,

My R knowledge could not take me any further, so this request !

I have a matrix of dimensions (1185 X 1185). I want to calculate standard
deviation of entire matrix. 
sd function of {stats} calculates standard deviation for each row/column,
giving 1 X 1185 matrix as result. I would like to have 1 X 1 matrix as
result.

Any ideas, how to do this ?

TIA
Chakri 

--
View this message in context: http://r.789695.n4.nabble.com/Standard-Deviation-of-a-matrix-tp3711991p3711991.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi!

The sample below should give you what you want:

M = matrix(runif(100), 10, 10)
sd(as.numeric(M))

So the as.numeric command is the key. It transforms the matrix to a 1D
vector. Or alternatively without using as.numeric:

M = matrix(runif(100), 10, 10)
M
dim(M) = 100
M
sd(M)

Here I use the dim command to set the dimensions to a vector of 100 long.

cheers,
Paul
On 08/02/2011 11:07 AM, chakri wrote:

  
    
#
Hi
or dim(M)<-NULL
long.
standard
row/column,
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html
#
On Aug 2, 2011, at 8:48 AM, Petr PIKAL wrote:

            
shortest would surely be:

sd( c(M) )
#
Thank you everyone for your kind input,

I forgot to add that I have decimal points in my matrix !

Enclosed input file (reduced to 10 X 10 matrix), scripts and output for your
suggesions:

Code 1:
library(stats)
Matrix<-read.table("test_input", head=T, sep=" ", dec=".")
SD<-sd(as.numeric(Matrix))
SD

Output 1:
Error in sd(as.numeric(Matrix)) : 
  (list) object cannot be coerced to type 'double'
Execution halted

Code 2:
library(stats)
Matrix<-read.table("test_input", head=T, sep="\t", dec=".")
dim(Matrix)<-1
SD<-sd(Matrix)
SD

Output:
Error in dim(Matrix) <- 1 : 
  dims [product 1] do not match the length of object [10]
Execution halted

Code 3:
library(stats)
Matrix<-read.table("test_input", head=T, sep="\t", dec=".")
SD<-sd(c(Matrix))
SD

Output:
Error: is.atomic(x) is not TRUE
Execution halted

Any ideas, what am I missing here ?

TIA
chakri
Input file:  http://r.789695.n4.nabble.com/file/n3712328/test_input
test_input 

--
View this message in context: http://r.789695.n4.nabble.com/Standard-Deviation-of-a-matrix-tp3711991p3712328.html
Sent from the R help mailing list archive at Nabble.com.
#
First, your data attachment did not come through the list.  Second, decimals are not a problem.  Third, you don't have a matrix, you have a data frame (read.table produces data frames).  As long as all columns are numeric you could do something like

sd(c(as.matrix(m)))

You could also convert to a matrix on input if you really don't need a dataframe for different column types.

Hope this is helpful,

Dan 

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204