Search code examples
rmatrix

How to check whether a matrix contains specific rows?


Consider that we have a matrix like below:

my_mat <- matrix(c(1, 2, 3, 4), 2, 2)

And, we have another matrix MM, which has dimension 50x2.

dim(MM)
[1] 50  2

Here, I want to check whether MM has rows that are the same as that of my_may.

That is, I want to check whether MM has the rows c(1, 3) or c(2, 4).

One direct way could be

my_mat[1, ] == t(MM)
my_mat[2, ] == t(MM)

But, in my problem, my_mat also has very large number of rows.

So, how can I do this compactly?


Solution

  • Maybe simply using base::merge() is enough to do what you want? For example:

    set.seed(123)
    my_mat <- matrix(c(1, 2, 3, 4), 2, 2)
    large_mat <- matrix(sample(1:4, 100, replace = TRUE), ncol = 2)
    
    head(large_mat)
    #>      [,1] [,2]
    #> [1,]    3    4
    #> [2,]    3    1
    #> [3,]    3    3
    #> [4,]    2    1
    #> [5,]    3    3
    #> [6,]    2    4
    
    any_common <- if (nrow(merge(large_mat, my_mat)) == 0) FALSE else TRUE
    any_common
    #> [1] TRUE
    

    If you want to know which rows in the large matrix match, then a bit more work is required:

    set.seed(123)
    my_mat <- matrix(c(1, 2, 3, 4), 2, 2)
    large_mat <- matrix(sample(1:4, 100, replace = TRUE), ncol = 2)
    large_mat <- as.data.frame(large_mat)
    large_mat$row_num <- 1:nrow(large_mat)
    
    head(large_mat)
    #>   V1 V2 row_num
    #> 1  3  4       1
    #> 2  3  1       2
    #> 3  3  3       3
    #> 4  2  1       4
    #> 5  3  3       5
    #> 6  2  4       6
    
    which_common <- merge(large_mat, my_mat)$row_num
    
    which_common
    #> [1] 14 23 25 12 13  6 30
    
    any_common <- if (length(which_common) == 0) FALSE else TRUE
    
    any_common
    #> [1] TRUE
    

    Created on 2023-11-01 with reprex v2.0.2