[R] matching rows in matrices

Ravi Varadhan RVaradhan at jhmi.edu
Thu Jul 2 17:10:09 CEST 2009


Here is another approach :

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)
}


#  example
a <- matrix(c(1,2,1,5,2,5), 2, 3)
b <- matrix(c(1,2,4,5,7,7,9,2, 5,5, 9,8,8,10,10, 5), 8, 2)

rowmatch.count(b, t(a))

Interestingly, this was suggested by either Marc or Gabor about 3 years ago
when I had the same question; you may be able to tack it down in r-help
archives.

Hope this helps,
Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Marc Schwartz
Sent: Thursday, July 02, 2009 10:20 AM
To: dreamworx
Cc: r-help at r-project.org
Subject: Re: [R] matching rows in matrices

On Jul 2, 2009, at 7:27 AM, dreamworx wrote:

>
> I have a matrix such as this,
>
>     [,1] [,2] [,3]
> [1,]    1    1    2
> [2,]    2    5    5
>
> and a larger matrix such as this,
>
>     a  b
> [1,] 1  5
> [2,] 2  5
> [3,] 4  9
> [4,] 5  8
> [5,] 7  8
> [6,] 7 10
> [7,] 9 10
>
> what I want to do is check the number of times the columns in the 
> first matrix appear as rows in the second matrix. So for instance in 
> this example
> 1,5  and 2,5 appear in both so I'd want R to return the number 2. Is 
> there a function to do this?


My first approach would be to use merge(), which performs a relational join.
So, given mat1:

# Note the rownames here, to match the colnames in mat2  > mat1
   [,1] [,2]
a    1    2
b    5    5


and mat2:

 > mat2
      a  b
[1,] 1  5
[2,] 2  5
[3,] 4  9
[4,] 5  8
[5,] 7  8
[6,] 7 10
[7,] 9 10


Using merge() we get:

 > merge(t(mat1), mat2)
   a b
1 1 5
2 2 5

This gives us the rows that match between the two matrices. Note that I
transpose mat1 so that the structure matches that of mat2.

Then just use nrow() to get a count:

 > nrow(merge(t(mat1), mat2))
[1] 2


See ?merge for more information.

HTH,

Marc Schwartz

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list