# Re: [R] Conditional Matrices

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Tue 16 Aug 2005 - 18:23:10 EST

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).
>

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

```
```#      [,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@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
```Date: 16-Aug-05                                       Time: 09:08:25
------------------------------ XFMail ------------------------------

______________________________________________
```
R-help@stat.math.ethz.ch mailing list