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.
Standard Deviation of a matrix
6 messages · Paul Hiemstra, PIKAL Petr, David Winsemius +2 more
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:
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.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Paul Hiemstra, Ph.D. Global Climate Division Royal Netherlands Meteorological Institute (KNMI) Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39 P.O. Box 201 | 3730 AE | De Bilt tel: +31 30 2206 494 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
Hi
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
or dim(M)<-NULL
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:
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.
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
-- Paul Hiemstra, Ph.D. Global Climate Division Royal Netherlands Meteorological Institute (KNMI) Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39 P.O. Box 201 | 3730 AE | De Bilt tel: +31 30 2206 494 http://intamap.geo.uu.nl/~paul http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
On Aug 2, 2011, at 8:48 AM, Petr PIKAL wrote:
Hi
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
or dim(M)<-NULL
shortest would surely be: sd( c(M) )
David.
>
>> 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:
>>> 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.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>
>>
>> --
>> Paul Hiemstra, Ph.D.
>> Global Climate Division
>> Royal Netherlands Meteorological Institute (KNMI)
>> Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
>> P.O. Box 201 | 3730 AE | De Bilt
>> tel: +31 30 2206 494
>>
>> http://intamap.geo.uu.nl/~paul
>> http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
West Hartford, CT
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:
library(stats)
Matrix<-read.table("test_input", head=T, sep="\t", dec=".")
SD<-sd(as.numeric(Matrix))
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:
library(stats)
Matrix<-read.table("test_input", head=T, sep="\t", dec=".")
dim(Matrix)<-1
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:
library(stats)
Matrix<-read.table("test_input", head=T, sep="\t", dec=".")
SD<-sd(c(Matrix))
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.
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
project.org] On Behalf Of chakri
Sent: Tuesday, August 02, 2011 6:31 AM
To: r-help at r-project.org
Subject: Re: [R] Standard Deviation of a matrix
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
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