[R] compare two matrices

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Sep 27 20:08:52 CEST 2010


one way is the following:

col1 <- c(1,2,3,4,5,6)
col2 <- c(6,5,4,3,2,1)
m <- cbind(col1, col2)
col3 <- c(1,3,2,6)
col4 <- c(6,3,5,1)
n <- cbind(col3, col4)

ind.n <- do.call(paste, c(as.data.frame(n), sep = "\r"))
ind.m <- do.call(paste, c(as.data.frame(m), sep = "\r"))
ind.n %in% ind.m


I hope it helps.

Best,
Dimitris


On 9/27/2010 8:00 PM, xinxin xx wrote:
>
> Hi everyone:
>
>
>
>    I have a kinda easy question but i do not know how to solve that in a simple way.
>
>    I want to compare the rows of two matrices.
>
>
>
>    col1<- c(1,2,3,4,5,6)
>
>    col2<- c(6,5,4,3,2,1)
>
>    m<-  cbind(col1, col2)
>
>
>
>    col3<- c(1,3,2,6)
>
>    col4<- c(6,3,5,1)
>
>    n<- cbind(col3, col4)
>
>
>
>    In matrix n,  for example the first row is (1,6), it is also some row in matrix m, i want the code results "TRUE".  then for the 2nd row (3,3), it should be FALSE.
>
>    So in this case it should be (TRUE, FALSE, TRUE, TRUE)
>
>
>
>    I tried %in% or is.element for a row in n and matrix m, but it does not work.
>
>    I think I can also write two loops to compare the rows of m and n one by one, but it takes a long time to run.
>
>    Could anyone tell me how to solve this?
>
>
>
>
>
> Thank you very much!!
>
>
>   		 	   		
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> 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 University 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