[R] Standard deviation for each element in a set of matrices
David Winsemius
dwinsemius at comcast.net
Fri Dec 4 19:39:42 CET 2009
On Dec 4, 2009, at 1:27 PM, Gray Calhoun wrote:
> Hi Muhammad,
> Load the data from all of the files into an array (probably using a
> for loop), then call apply on the resulting array: ie
I like that idea a lot and it does not need an explicit loop:
apply( array(c(File1, File2, File3), dim=c(3,3,3) ) , 1:2, sd)
I had come up with this rather messy solution before seeing your much
more elegant one:
matrix( apply(sapply(list(File1, File2, File3), "["), 1,sd),
nrow(File1), ncol(File1) )
--
David
>
> ## replace the first line with code to load your data
>> rr <- array(c(rep(1:3,3), rep(4:6,3), rep(7:9,3)), c(3,3,3))
>> apply(rr, c(1,2), sd)
> [,1] [,2] [,3]
> [1,] 3 3 3
> [2,] 3 3 3
> [3,] 3 3 3
>
> --Gray
>
> On Fri, Dec 4, 2009 at 11:56 AM, Muhammad Rahiz
> <muhammad.rahiz at ouce.ox.ac.uk> wrote:
>> Hello R-users,
>>
>> I would like to know how to find the standard deviation for each
>> element in
>> a set of matrices.
>>
>> Given the following files,
>>
>> File1 File2 File3
>> 1 1 1 4 4 4 7 7 7
>> 2 2 2 5 5 5 8 8 8
>> 3 3 3 6 6 6 9 9 9
>>
>> I want to calculate the standard deviation for every cell in the
>> file (I'm
>> using gridded data). The desired output is
>>
>> Output
>> 3 3 3
>> 3 3 3
>> 3 3 3
>>
>>
>> Thanks.
>>
>> Muhammad
>>
>> --
>> Muhammad Rahiz | Doctoral Student in Regional Climate Modeling
>>
>> Climate Research Laboratory, School of Geography & the Environment
>> Oxford University Centre for the Environment
>> South Parks Road, Oxford, OX1 3QY, United Kingdom Tel: +44
>> (0)1865-285194
>> Mobile: +44 (0)7854-625974
>> Email: muhammad.rahiz at ouce.ox.ac.uk
>>
>> ______________________________________________
>> 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.
>>
>
>
>
> --
> Gray Calhoun
>
> Assistant Professor of Economics
> Iowa State University
>
> ______________________________________________
> 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
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list