Dear all, Suppose that I have a matrix A A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) and a logical matrix B B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) The result matrix should be C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) Is there any simple tip or trick to perform this without looping? Thanks in advance for any suggestion. Best regards, Andrej
try this: A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) C <- A C[!B] <- NA C I hope it helps. Best, Dimitris Andrej Kastrin wrote:> Dear all, > > Suppose that I have a matrix A > > A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) > > and a logical matrix B > > B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) > > The result matrix should be > > C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) > > Is there any simple tip or trick to perform this without looping? > > Thanks in advance for any suggestion. > > Best regards, Andrej > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014
Use the is.na() function to assign NA values:> is.na(A) <- !B > A[,1] [,2] [,3] [1,] 3 NA NA [2,] 3 3 3 [3,] 3 3 NA> C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) > all.equal(A, C)[1] TRUE Steven McKinney Statistician Molecular Oncology and Breast Cancer Program British Columbia Cancer Research Centre email: smckinney +at+ bccrc +dot+ ca tel: 604-675-8000 x7561 BCCRC Molecular Oncology 675 West 10th Ave, Floor 4 Vancouver B.C. V5Z 1L3 Canada -----Original Message----- From: r-help-bounces at r-project.org on behalf of Dimitris Rizopoulos Sent: Mon 1/19/2009 12:54 PM To: Andrej Kastrin Cc: r-help at r-project.org Subject: Re: [R] Compare matrices try this: A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) C <- A C[!B] <- NA C I hope it helps. Best, Dimitris Andrej Kastrin wrote:> Dear all, > > Suppose that I have a matrix A > > A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) > > and a logical matrix B > > B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) > > The result matrix should be > > C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) > > Is there any simple tip or trick to perform this without looping? > > Thanks in advance for any suggestion. > > Best regards, Andrej > > ______________________________________________ > 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. >-- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 ______________________________________________ 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.
On 20/01/2009, at 9:48 AM, Andrej Kastrin wrote:> Dear all, > > Suppose that I have a matrix A > > A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) > > and a logical matrix B > > B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) > > The result matrix should be > > C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) > > Is there any simple tip or trick to perform this without looping?(a) It is helpful to state a clear question rather than expecting the reader to infer from an example just what your question is. (b) It is advisable to use TRUE and FALSE as the values of a logical variate, rather than T and F. The former are reserved words, the latter are not, which can cause all hell to break loose. (c) You are wasting a good many key strokes. You could simply say A <- matrix(3,3,3) (d) I infer that what you want is for C to equal A where B is TRUE and NA otherwise. One way to accomplish this is: C <- matrix(,3,3) C[B] <- A[B] cheers, Rolf Turner ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
On Jan 19, 2009, at 3:54 PM, Dimitris Rizopoulos wrote:> try this: > > A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) > B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) > > C <- A > C[!B] <- NA > C >Very elegant. Another, perhaps less elegant, effort: B[which(B == FALSE)] <- NA > B [,1] [,2] [,3] [1,] TRUE NA NA [2,] TRUE TRUE TRUE [3,] TRUE TRUE NA > C <- matrix(A * B, 3,3) # A * B is *not* matrix multiplication > > > C [,1] [,2] [,3] [1,] 3 NA NA [2,] 3 3 3 [3,] 3 3 NA -- David Winsemius> > I hope it helps. > > Best, > Dimitris > > > Andrej Kastrin wrote: >> Dear all, >> Suppose that I have a matrix A >> A <- matrix(c(3,3,3,3,3,3,3,3,3),3,3) >> and a logical matrix B >> B <- matrix(c(T,T,T,F,T,T,F,T,F),3,3) >> The result matrix should be >> C <- matrix(c(3,3,3,NA,3,3,NA,3,NA),3,3) >> Is there any simple tip or trick to perform this without looping? >> Thanks in advance for any suggestion. >> Best regards, Andrej >> ______________________________________________ >> 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. > > -- > Dimitris Rizopoulos > Assistant Professor > Department of Biostatistics > Erasmus Medical Center > > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands > Tel: +31/(0)10/7043478 > Fax: +31/(0)10/7043014 > > ______________________________________________ > 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.