[R] comparing two matrices, row by row

Finny Kuruvilla kuruvill at fas.harvard.edu
Thu Apr 26 18:55:12 CEST 2007


Another similar way to do this using apply is:

apply(ar1==ar2,1,all)

Best,
Finny Kuruvilla

*****************************************************************
Finny Kuruvilla, MD, PhD
Harvard Medical School Fellowship Program in Transfusion Medicine
Broad Institute of MIT and Harvard
Homepage: http://www.people.fas.harvard.edu/~kuruvill/home/

> Estimated people,
> 
> I have two matrices:
> 
> ar1 <- array(data=c(1:16),dim=c(4,4))
> ar2 <- array(data=c(1,2,3,3,5:16),dim=c(4,4))
> 
> They only differ in the fourth row. I would like to compare them in
> order to know which columns are equal.
> 
> The following works, but I would like to have a better solution, and
> not
> to use what someone called "prehistorical loops":
> 
> for(i in c(1:4)) { cat(as.character(i),": ",
> as.character(setequal(ar1[i,],ar2[i,])), "\n") }
> 1 :  TRUE
> 2 :  TRUE
> 3 :  TRUE
> 4 :  FALSE
> 
> I cannot devise how to use the apply function for this.
> Thanks a lot,
> Federico



More information about the R-help mailing list