[R] Standard deviation for each element in a set of matrices

Phil Spector spector at stat.berkeley.edu
Fri Dec 4 19:19:13 CET 2009


Muhammad -
    Here's one way:

files = c('File1','File2','File3')
themats = lapply(files,read.table)
ans = matrix(0,3,3)
for(i in 1:3)for(j in 1:3)ans[i,j] = sd(sapply(themats,function(x)x[i,j]))

    Here's another

files = c('File1','File2','File3')
themats = lapply(files,read.table)
ans = outer(1:3,1:3,Vectorize(function(i,j)sd(sapply(themats,function(x)x[i,j]))))


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Fri, 4 Dec 2009, Muhammad Rahiz 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.
>




More information about the R-help mailing list