[R] Intersection of 2 matrices
Michael Kao
mkao006rmail at gmail.com
Fri Dec 2 18:47:07 CET 2011
On 2/12/2011 2:48 p.m., David Winsemius wrote:
>
> On Dec 2, 2011, at 4:20 AM, oluwole oyebamiji wrote:
>
>> Hi all,
>> I have matrix A of 67420 by 2 and another matrix B of 59199 by 2.
>> I would like to find the number of rows of matrix B that I can find
>> in matrix A (rows that are common to both matrices with or without
>> sorting).
>>
>> I have tried the "intersection" and "is.element" functions in R but
>> it only working for the vectors and not matrix
>> i.e, intersection(A,B) and is.element(A,B).
>
> Have you considered the 'duplicated' function?
>
Here is an example based on the duplicated function
test.mat1 <- matrix(1:20, nc = 5)
test.mat2 <- rbind(test.mat1[sample(1:5, 2), ], matrix(101:120, nc = 5))
compMat <- function(mat1, mat2){
nr1 <- nrow(mat1)
nr2 <- nrow(mat2)
mat2[duplicated(rbind(mat1, mat2))[(nr1 + 1):(nr1 + nr2)], ]
}
compMat(test.mat1, test.mat2)
More information about the R-help
mailing list