Skip to content

Selecting rows that are the same in separate data frames

5 messages · Bart Joosen, Jorge Ivan Velez, Henrique Dallazuanna +1 more

#
I want to compare two matrices or data frames and select or get an index for
those rows which are the same in both. I have tried the following :






a = matrix ( 1:10, ncol = 2 )
a

b = matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 )
b

a[a==b]






a = as.data.frame ( matrix ( 1:10, ncol = 2 ) )
a

b = as.data.frame ( matrix ( c ( 2,3,4,7,8,9 ), ncol = 2 ) )
b

a[a==b]








Any ideas please.


Thanks.


Simon Parker
Imperial College
#
I'm not sure what you want, but take a look at ?merge and %in%
ppaarrkk wrote:

  
    
#
Thanks for reply.

What I want is the equivalent of this :

xxx = 1:10
which(xxx %in% c(2,5))


.......but where there is more than one criterion for matching.


which (b %in% a) in the code I included does nothing (not surprisingly).

I'm not sure that I can use merge, because I want the whole of a, but to
mark those rows which are also in b. If I do merge ( a,b ),  I just get b.
If I do merge ( a,b, all.x =TRUE), I get a.
bartjoosen wrote: