Hi, I have three matrices which could be, for example: A = 0, NA NA, 3 B = 1, NA 0, NA C = 1, NA 1, 1 (The point is that they all may have NA's in some cells) QUESTION: How do I perform a element-by-element sum of the elements of these three matrices (A + B + C), ignoring NA's, to obtain: D = 2, NA 1, 4 In reality I am handling much larger matrices (not just 2x2). Thank you for any help! [[alternative HTML version deleted]]
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 A.K. ----- Original Message ----- From: Thiago Couto <couto.thiagoba at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, July 23, 2012 4:47 PM Subject: [R] help with element-by-element sum with NA Hi, ? ? I have three matrices which could be, for example: ? ? A =? 0, NA ? ? ? ? ? NA, 3 ? ? B =? 1, NA ? ? ? ? ? ? 0, NA ? ? C = 1, NA ? ? ? ? ? ? 1, 1 ? ? (The point is that they all may have NA's in some cells) ? ? QUESTION: How do I perform a element-by-element sum of the elements of ? ? these three matrices (A + B + C), ignoring NA's, to obtain: ? ? D = 2, NA ? ? ? ? ? 1, 4 ? ? In reality I am handling much larger matrices (not just 2x2). ? ? Thank you for any help! ??? [[alternative HTML version deleted]] ______________________________________________ R-help at 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.
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 <couto.thiagoba at gmail.com> wrote:> Hi, > > I have three matrices which could be, for example: > A = 0, NA > NA, 3 > > B = 1, NA > 0, NA > > C = 1, NA > 1, 1 > > (The point is that they all may have NA's in some cells) > > QUESTION: How do I perform a element-by-element sum of the elements of > these three matrices (A + B + C), ignoring NA's, to obtain: > > D = 2, NA > 1, 4 > > In reality I am handling much larger matrices (not just 2x2). > > Thank you for any help! > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Possibly Parallel Threads
- add/subtract matrices, ignoring NA or missing values
- sum() with na.rm=TRUE, again
- return NA
- ans[nas] <- NA in 'ifelse' (was: ifelse() woes ... can we agree on a ifelse2() ?)
- using ifelse to remove NA's from specific columns of a data frame containing strings and numbers