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 <- 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)
na.v<-is.na(v)
ifelse(na.u & na.v, NA, ifelse(na.u, 0, u)+ ifelse(na.v,0,v))
}
Reduce('matrixadder',mymats)
******************************
Cheers,
Greg
On Sep 12, 2010, at 8:33 PM, Ben Bolker wrote:
> 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.
>
> ______________________________________________
> [email protected] 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.
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.