[R] Compare matrices
Steven McKinney
smckinney at bccrc.ca
Mon Jan 19 22:02:57 CET 2009
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.
More information about the R-help
mailing list