Is this what you want:
> a
[,1] [,2]
[1,] "abc" "a1"
[2,] "abc" "d2"
[3,] "bcd" "d1"
[4,] "bcd" "d2"
[5,] "bce" "a1"
[6,] "bce" "a2"> b
[,1] [,2]
[1,] "a1" "a2"
[2,] "a1" "d2"> x <- lapply(split(seq(nrow(a)), a[,1]), function(.indx){
+ do.call(rbind, lapply(seq(nrow(b)), function(.row){
+ if (any(b[.row, 1] == a[.indx, 2]) && any(b[.row, 2] ==
a[.indx, 2])){
+ c(a[.indx[1],1], b[.row,])
+ } else NULL
+ }))
+ })> do.call(rbind, x)
[,1] [,2] [,3]
[1,] "abc" "a1" "d2"
[2,] "bce" "a1" "a2">
On Wed, Dec 3, 2008 at 7:18 AM, T Joshi <tejalonline at gmail.com>
wrote:> Hi,
>
> I have two matrices as follow:
> matrix A >
>
a=matrix(c(c("abc","abc","bcd","bcd","bce","bce"),c("a1","d2","d1","d2","a1","a2")),6,2)
>
> and matrix B which contains pair of values :
>
b=matrix(c(c("a1","a1"),c("a2","d2")),2,2)
>
>
> In short, I wish to find out pairs of values in matrix a[,2] having
> same value in a[,1], which occur as a row in matrix b, so that the
> output becomes :
> abc
> bce, or
>
> even better
> abc a1 d2
> bce a1 a2
>
> How can I do that?
> cheers,
> Joshi
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?