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

Arnar Flatberg arnar.flatberg@gmail....
Thu Jul 24 08:49:32 CDT 2008


On Thu, Jul 24, 2008 at 3:00 PM, Wes McKinney <wesmckinn@gmail.com> 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:
>
> > allData <- cbind(c(1,2,3,4,5), c(12, 19, 27, 38, 51))
> > allData
>      [,1] [,2]
> [1,]    1   12
> [2,]    2   19
> [3,]    3   27
> [4,]    4   38
> [5,]    5   51
> > subData <- cbind(c(3,5,1), c(NA, NA, NA))
> > subData
>      [,1] [,2]
> [1,]    3   NA
> [2,]    5   NA
> [3,]    1   NA
>

What about using `intersect` combined with `where` ?

all_data = np.array([[1,2,3,4,5], [12,19,27,38,51]]).T
sub_data = np.array([[3,5,1], [nan,nan,nan]]).T
match_ind = np.where(np.intersect_1d(sub_data[:,0], all_data[:,0]))
sub_data[:,1] = all_data[match_ind,1]

It may not be pretty or the best approach for solving the above examples but
it behaves like R's match somewhat.

Arnar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20080724/ca5abbda/attachment.html 


More information about the SciPy-user mailing list