Suppose I have two matrices, each with two columns and differing numbers of row. I want to check and see which pairs of one matrix are in the other matrix. If these were one
Recreate your data:
a <- matrix(c(1, 2, 4, 9, 1, 6, 7, 7), ncol=2, byrow=TRUE)
x <- matrix(c(1, 6, 2, 7, 3, 8, 4, 9, 5, 10), ncol=2, byrow=TRUE)
Define a function %inm% that is a matrix analogue to %in%:
`%inm%` <- function(x, matrix){
test <- apply(matrix, 1, `==`, x)
any(apply(test, 2, all))
}
Apply this to your data:
apply(a, 1, `%inm%`, x)
[1] FALSE TRUE TRUE FALSE
To compare a single row:
a[1, ] %inm% x
[1] FALSE
a[2, ] %inm% x
[1] TRUE