[R] Conditional Matrices

(Ted Harding) Ted.Harding at nessie.mcc.ac.uk
Tue Aug 16 10:23:10 CEST 2005


On 16-Aug-05 ISAIAH SHALWITZ wrote:
> This seems like a simple problem but I can't figure it out:
> 
> I have two identical DIMENSION matrices.  Both contain only binary
> values NOT identical between matrices.  What I want to do: If in cell
> (1,1) the value in the first matrix (x) equals 1, then I keep the value
> in cell (1,1) in the second matrix (y).  If in cell (1,1) the value in
> the first matrix (x) equals 0, then I change the value in cell (1,1) in
> the second matrix (y)to missing (NA).  Repeat for every pair of cells
> (coordinates of the paired cells always match).
> 
> Please help.

It seems the following is what you are looking for:

A<-matrix(c(1,0,1,0,1,1,1,1,1),nrow=3)
A
#      [,1] [,2] [,3]
# [1,]    1    0    1
# [2,]    0    1    1
# [3,]    1    1    1

B<-matrix(c(1,2,3,4,5,6,7,8,9),nrow=3)
B
#      [,1] [,2] [,3]
# [1,]    1    4    7
# [2,]    2    5    8
# [3,]    3    6    9

B[A==0]<-NA
B
#      [,1] [,2] [,3]
# [1,]    1   NA    7
# [2,]   NA    5    8
# [3,]    3    6    9

Best wishes,
Ted.


--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 16-Aug-05                                       Time: 09:08:25
------------------------------ XFMail ------------------------------




More information about the R-help mailing list