Re: [R] help with element-by-element sum with NA

2012-07-24 Thread Peter Ehlers
On 2012-07-23 21:48, R. Michael Weylandt wrote: Perhaps something like: Reduce(function(x,y){x[is.na(x)] <- 0; y[is.na(y)] <- 0; x + y}, list(A,B,C)) Not the most elegant, but it will get the job done. Michael I like Reduce(), but here are a couple more solutions: 1. tmp <- mapply(FUN = s

Re: [R] help with element-by-element sum with NA

2012-07-23 Thread arun
HI, Try this: A<-matrix(c(0,NA,NA,3),ncol=2) B<-matrix(c(1,0,NA,NA),ncol=2)  C<-matrix(c(1,1,NA,1),ncol=2) AB<-ifelse(is.na(A),ifelse(is.na(B),NA,B), ifelse(is.na(B), A, A+B))  ABC<-ifelse(is.na(AB),ifelse(is.na(C),NA,C),ifelse(is.na(C),AB,AB+C))  ABC [,1] [,2] [1,]    2   NA [2,]    1    4

Re: [R] help with element-by-element sum with NA

2012-07-23 Thread R. Michael Weylandt
Perhaps something like: Reduce(function(x,y){x[is.na(x)] <- 0; y[is.na(y)] <- 0; x + y}, list(A,B,C)) Not the most elegant, but it will get the job done. Michael On Mon, Jul 23, 2012 at 3:47 PM, Thiago Couto wrote: > Hi, > > I have three matrices which could be, for example: > A = 0,