Re: [R] average matrices across a list

2010-09-14 Thread Ben Bolker
Gregory Ryslik rsaber at comcast.net writes: mymats - vector('list', 5) set.seed(246) # Generate a list of five 3 x 3 matrices for(i in 1:5) mymats[[i]] - matrix(sample(1:9), nrow = 3) mymats[[5]][1,1]-NA mymats[[4]][2,2]-NA mymats matrixadder-function(u,v){ na.u-is.na(u)

Re: [R] average matrices across a list

2010-09-14 Thread Henrique Dallazuanna
You can try this also: Reduce('+', lapply(mymats, function(x)replace(x, is.na(x), 0))) On Sun, Sep 12, 2010 at 10:36 PM, Gregory Ryslik rsa...@comcast.net wrote: Hi Everyone, Thanks to everyone for their help. With your suggestions and some poking around, the following works for what I

Re: [R] average matrices across a list

2010-09-14 Thread Dennis Murphy
Hi: It's certainly fast (0.97s for 1 reps on my box), but doesn't replacement by zero affect the denominator of the sum, thereby deflating the means (assuming the contents of the matrices are nonnegative or NA)? Dennis On Tue, Sep 14, 2010 at 5:28 AM, Henrique Dallazuanna

Re: [R] average matrices across a list

2010-09-14 Thread Gregory Ryslik
Hi, The denominator i compute seperately counting how many observations there were that were not NA. Thus the I divide each (n,m) cell by the number of counts it was not NA. Thanks, Greg On Sep 14, 2010, at 9:23 AM, Dennis Murphy wrote: Hi: It's certainly fast (0.97s for 1 reps on my

[R] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi, I have a list of several hundred 2 dimensional matrices, where each matrix is n x m. What I need to do is that for each n,m I need an average over all the lists. This would collapse it down to just one nxm matrix. Any easy ways to do that? As always, I'd like to avoid a for loop to keep

Re: [R] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Sorry, I forgot to add that some of the entries in various matrices have NA in them. On Sep 12, 2010, at 3:40 PM, Gregory Ryslik wrote: Hi, I have a list of several hundred 2 dimensional matrices, where each matrix is n x m. What I need to do is that for each n,m I need an average over

Re: [R] average matrices across a list

2010-09-12 Thread Phil Spector
Gregory - Suppose your list is called mymats. Then Reduce(+,mymats) does what you want. - Phil On Sun, 12 Sep 2010, Gregory Ryslik wrote: Hi, I have a list of several hundred 2 dimensional matrices, where each matrix is n x m. What I need to do is

Re: [R] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi, Doing that I get the following: Browse[2] Reduce[+,results] Error in Reduce[+, results] : object of type 'closure' is not subsettable Thanks again! Kind regards, Greg On Sep 12, 2010, at 3:49 PM, Phil Spector wrote: Gregory - Suppose your list is called mymats. Then

Re: [R] average matrices across a list

2010-09-12 Thread Ben Bolker
Gregory Ryslik rsaber at comcast.net writes: Browse[2] Reduce[+,results] Error in Reduce[+, results] : object of type 'closure' is not subsettable You need to use parentheses, not square brackets. __ R-help@r-project.org mailing list

Re: [R] average matrices across a list

2010-09-12 Thread Phil Spector
Gregory - Please provide a reproducible example. I have no idea what results is. - Phil On Sun, 12 Sep 2010, Gregory Ryslik wrote: Hi, Doing that I get the following: Browse[2] Reduce[+,results] Error in Reduce[+, results] : object of type 'closure' is

Re: [R] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi, Thanks, I was using the square brackets instead of (. The ( makes it work. However, some of my matrices have NA for some values. I need those NA's to basically not be counted. And if all the lists have NA for a specific (n,m), I want it to remain an (n,m). By using the Reduce('+', mymats),

Re: [R] average matrices across a list

2010-09-12 Thread Ben Bolker
My next suggestion (I don't have time to work out or test an example at the moment): library(abind) tmparr - abind(m1,m2,m3,...,along=3) OR tmparr - do.call(c(matlist,list(along=3))) apply(tmparr,c(1,2),mean,na.rm=TRUE) or something along those lines.

Re: [R] average matrices across a list

2010-09-12 Thread Gregory Ryslik
Hi Everyone, Thanks to everyone for their help. With your suggestions and some poking around, the following works for what I need. It basically adds all the matrices elementwise, and adds nothing if the element is NA. Thanks again! Code below: ** mymats -