Skip to content
Prev 257784 / 398503 Next

Matching a vector with a matrix row

Here is one solution:

rowmatch <- function(A,B) { 
# Rows in A that match the rows in B
    f <- function(...) paste(..., sep=":")
   if(!is.matrix(B)) B <- matrix(B, 1, length(B))
    a <- do.call("f", as.data.frame(A))
    b <- do.call("f", as.data.frame(B))
    match(b, a)
}

A <- matrix(1:1000, 100, 10, byrow=TRUE)
B <- matrix(21:40, 2, 10, byrow=TRUE)
rowmatch(A, B )

b <- 51:60
rowmatch(A, b)

Ravi.