[R] Compare matrices

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Jan 19 21:54:31 CET 2009


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




More information about the R-help mailing list