Re: [R] add/subtract matrices, ignoring NA or missing values

2008-01-28 Thread Gabor Grothendieck
Perhaps you could explain the motivation behind this. At any rate here are three different solutions: ifelse(is.na(m1), ifelse(is.na(m2), NA, m2), ifelse(is.na(m2), m1, m1 + m2)) apply(array(c(m1, m2), c(2,2,2)), 1:2, function(x) sum(c(na.omit(x), NA)[1])) na.m1 <- is.na(m1) na.m2 <- is.na(m2)

Re: [R] add/subtract matrices, ignoring NA or missing values

2008-01-28 Thread Rolf Turner
On 29/01/2008, at 3:34 PM, Ng Stanley wrote: > Hi, > > For example, given two 2x2 matrices m1 and m2. I would like to add/ > subtract > element by element > >> m1 > [,1] [,2] > [1,] NA NA > [2,]12 > >> m2 > [,1] [,2] > [1,]1 NA > [2,] NA2 > >> m1 + m2 >

[R] add/subtract matrices, ignoring NA or missing values

2008-01-28 Thread Ng Stanley
Hi, For example, given two 2x2 matrices m1 and m2. I would like to add/subtract element by element > m1 [,1] [,2] [1,] NA NA [2,]12 > m2 [,1] [,2] [1,]1 NA [2,] NA2 > m1 + m2 [,1] [,2] [1,] NA NA [2,] NA4 How can I ignore the NA, and g