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. I
Conditional Matrices
4 messages · ISAIAH SHALWITZ, Patrick Burns, (Ted Harding) +1 more
ifelse(mat1, mat2, NA) should do what you want. Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User")
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. I
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
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 ------------------------------
#M1 = first matrix #M2 = second matrix M2[M1==0]<-NA ----- Original Message ----- From: "ISAIAH SHALWITZ" <shalwitz.1 at osu.edu> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, August 16, 2005 5:00 AM Subject: [R] Conditional Matrices
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. I
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html