# [R] matching type question, please

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu Dec 16 23:01:32 CET 2021

```I am not sure Eric's solution is what is wanted:

Consider:
xr1 <- c(1,2,1)
xr2 <- c(4,5,4)
xs1 <- c(6,6)
xs2 <- c(7,7)

> z1 <- cbind(xr1, xr2)
> z2 <- cbind(xs1,xs2)
> z1
xr1 xr2
[1,]   1   4
[2,]   2   5
[3,]   1   4
> z2
xs1 xs2
[1,]   6   7
[2,]   6   7

If what is wanted is to find rows of z2 that match those in z1, Eric's
proposal gives (note the added comma to give a logical indexing
vector):

> a <- cbind(c(xr1,xs1),c(xr2,xs2))
> a[duplicated(a),]
[,1] [,2]
[1,]    1    4
[2,]    6    7

This is obviously wrong, as it gives duplicates *within* z1 and z2,
not between them. To get rows of z2 that appear as duplicates of rows
of z1, then something like the following should do:

> a <- rbind(unique(z1),unique(z2))
> a
xr1 xr2
[1,]   1   4
[2,]   2   5
[3,]   6   7
> a[duplicated(a),]
xr1 xr2
## nothing

I leave it to Erin to determine whether this is relevant to her
problem and, if so, how to fix up my suggestion appropriately.

Cheers,
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )

On Thu, Dec 16, 2021 at 12:39 PM Eric Berger <ericjberger using gmail.com> wrote:
>
> > a <- cbind(c(xr1,xs1),c(xr2,xs2))
> > a[duplicated(a)]
> [1] 4 4
>
>
> On Thu, Dec 16, 2021 at 10:18 PM Erin Hodgess <erinm.hodgess using gmail.com> wrote:
> >
> > Hello!
> >
> > I have the following:
> >
> >  cbind(xr1,xr2)
> >
> >       xr1 xr2
> >
> >  [1,]   8   0
> >
> >  [2,]   7   1
> >
> >  [3,]   6   2
> >
> >  [4,]   5   3
> >
> >  [5,]   4   4
> >
> >  [6,]   3   5
> >
> >  [7,]   2   6
> >
> >  [8,]   1   7
> >
> >  [9,]   0   8
> >
> > > cbind(xs1,xs2)
> >
> >      xs1 xs2
> >
> > [1,]   9   4
> >
> > [2,]   8   4
> >
> > [3,]   7   4
> >
> > [4,]   6   4
> >
> > [5,]   5   4
> >
> > [6,]   4   4
> >
> > [7,]   3   4
> >
> > >
> >
> > These are ordered pairs.  I would like to get something that shows that the
> > pair (4,4) appears in both.  I have tried cbind with match and %in% and
> > intersect, but not getting the exact results.
> >
> > Any suggestions would be appreciated.  I have a feeling that it's something
> > really easy that I'm just not seeing.
> >
> > Thanks,
> > Erin
> >
> >
> > Erin Hodgess, PhD
> > mailto: erinm.hodgess using gmail.com
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help