# [SciPy-user] Equivalent to 'match' function in R?

Yosef Meller yosefmel@post.tau.ac...
Thu Jul 24 08:35:53 CDT 2008

```On Thursday 24 July 2008 16:00:38 Wes McKinney wrote:
> Hi all,
>
> I've been working with users lately who are transitioning from using R to
> NumPy/Scipy. Some are accustomed to using the 'match' function, for
>
> example:
[snipped outputs]
> > allData <- cbind(c(1,2,3,4,5), c(12, 19, 27, 38, 51))
> > subData <- cbind(c(3,5,1), c(NA, NA, NA))
> > matchInd <- match(subData[,1], allData[,1])
> > subData[,2] <- allData[matchInd,2]
> > allData <- cbind(c(1,2,3,4,5), rep(NA,5))
> > subData <- cbind(c(3,5,1), c(27, 51, 12))
> > matchInd <- match(subData[,1], allData[,1])
> > allData[matchInd,2] <- subData[,2]
> > allData
>
>      [,1] [,2]
> [1,]    1   12
> [2,]    2   NA
> [3,]    3   27
> [4,]    4   NA
> [5,]    5   51
>
> Before I reinvent the wheel, was curious if there was an equivalent
> function elsewhere already. If not then some Cython/Pyrex is in order.
>
> Thanks,
> Wes

If I understand this correctly, this is what you want:
---
from scipy import c_, r_, equal, nan

vals = r_[12, 19, 27, 38, 51]
selector = r_[3, 5, 1]
idxs = r_[1, 2, 3, 4, 5] # I assume these don't have to be consecutive.
mask = equal.outer(idxs, selector).any(axis=1)

alldata = c_[idxs, where(mask, vals, nan]
---
Maybe looks a little less obvious, but does the job.
```