Maybe there is a more elegant solution, but here is one possibility: mat1[is.na(mat1)]<-mat2[is.na(mat1)] mat2[is.na(mat2)]<-mat1[is.na(mat2)]
(mat1+mat2)/2 On Nov 21, 2007 12:30 PM, Gregory Gentlemen <[EMAIL PROTECTED]> wrote: > Hello fellow R users, > > I have a matrix computation that I imagine should be relatively easy to do, > however I cannot figure out a nice way to do it. I have two matrices, for > example > > mat1 <- matrix(c(1:5,rep(NA,5), 6:10), nrow=3, byrow=T) > mat2 <- matrix(c(2:6, 6:10, rep(NA,5)), nrow=3, byrow=T) > > I'd like to compute the element-wise average for non-NA entries. Of course > > (mat1+mat2)/2 > > does not work for the final two rows because of the NA's. Are there any > elegant ways to accopmlish this without writing a loop with indices? > > Thanks in advance for any assistance. > > Greg > > > > --------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. > -- Matthew C Keller Asst. Professor of Psychology University of Colorado at Boulder www.matthewckeller.com ______________________________________________ R-help@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.