Skip to content
Prev 75554 / 398502 Next

Vector comparison to matrix

Hi Todd,

Here is a function that was suggested to me by Gabor Grothendieck.  This
function counts the number of times each row of a matrix B occurs in another
matrix A.

rowmatch.count <- function(a,b) { 
    f <- function(...) paste(..., sep=":")
    a2 <- do.call("f", as.data.frame(a))
    b2 <- do.call("f", as.data.frame(b))
    c(table(c(a2,unique(b2)))[b2] - 1)
}

If you are interested in finding the number of occurrences of a vector "b"
instead, you can call this function as follows:

rowmatch.count(A,t(as.matrix(b))

Hope this is helps,
Ravi.