Re: [R] how to calculate means of matrix elements

2009-05-20 Thread Gabor Grothendieck
In that case use a modification of Jim's solution: a <- array(cbind(mat1, mat2, mat3), c(3, 3, 3)) apply(a, 1:2, mean, na.rm = TRUE) On Tue, May 19, 2009 at 8:49 AM, dxc13 wrote: > > Easy enough.  What if some of the matrix elements contained missing values? > Then how could you still calculate

Re: [R] how to calculate means of matrix elements

2009-05-19 Thread dxc13
Easy enough. What if some of the matrix elements contained missing values? Then how could you still calculate the means? Example code below: mat1 <- matrix(c(1,2,3,4,5,NA,7,8,9),3,3) mat2 <- matrix(c(NA,6,1,9,0,5,8,2,7),3,3) mat3 <- matrix(c(5,9,1,8,NA,3,7,2,4),3,3) Gabor Grothendieck wrote:

Re: [R] how to calculate means of matrix elements

2009-05-18 Thread Chris Stubben
dxc13 wrote: > > If these three matrices are given in separate text files, how can I > write code that will get this result I need? > If you have matrices in separate text files like mat1.txt, mat2.txt, mat3.txt, you could load them into a list using a loop x<- vector('list', 3) for ( i i

Re: [R] how to calculate means of matrix elements

2009-05-18 Thread Gabor Grothendieck
Try this: (mat1 + mat2 + mat3) / 3 On Mon, May 18, 2009 at 8:40 PM, dxc13 wrote: > > useR's, > I have several matrices of size 4x4 that I want to calculate means of their > respective positions with.  For example, consider I have 3 matrices given by > the code: > mat1 <- matrix(sample(1:20,16,re

Re: [R] how to calculate means of matrix elements

2009-05-18 Thread jim holtman
You can convert it to an array and then use apply: > mat1 [,1] [,2] [,3] [,4] [1,]32 124 [2,] 14 13 132 [3,] 15969 [4,]2 15 13 19 > mat2 [,1] [,2] [,3] [,4] [1,]0 11 107 [2,] 1293 13 [3,] -4 130 14 [4,] -

[R] how to calculate means of matrix elements

2009-05-18 Thread dxc13
useR's, I have several matrices of size 4x4 that I want to calculate means of their respective positions with. For example, consider I have 3 matrices given by the code: mat1 <- matrix(sample(1:20,16,replace=T),4,4) mat2 <- matrix(sample(-5:15,16,replace=T),4,4) mat3 <- matrix(sample(5:25,16,repl