Muhammad Rahiz
2009-Dec-04 17:56 UTC
[R] Standard deviation for each element in a set of matrices
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
Phil Spector
2009-Dec-04 18:19 UTC
[R] Standard deviation for each element in a set of matrices
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. >
Gray Calhoun
2009-Dec-04 18:27 UTC
[R] Standard deviation for each element in a set of matrices
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 ## 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