Skip to content

Commit 4c76f89

Browse files
committed
DOC: r match function
1 parent 0bab303 commit 4c76f89

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

doc/source/comparison_with_r.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,44 @@ function.
6666
For more details and examples see :ref:`the groupby documentation
6767
<groupby.split>`.
6868

69+
|match|_
70+
~~~~~~~~~~~~
71+
72+
A common way to select data in R is using ``%in%`` which is defined using the
73+
function ``match``. The operator ``%in%`` is used to return a logical vector
74+
indicating if there is a match or not:
75+
76+
.. code-block:: r
77+
78+
s <- 0:4
79+
s %in% c(2,4)
80+
81+
The :meth:`~pandas.DataFrame.isin` method is similar to R ``%in%`` operator:
82+
83+
.. ipython:: python
84+
85+
s = pd.Series(np.arange(5),index=np.arange(5)[::-1],dtype=np.float32)
86+
s.isin([2, 4])
87+
88+
The ``match`` function returns a vector of the positions of matches
89+
of its first argument in its second:
90+
91+
.. code-block:: r
92+
93+
s <- 0:4
94+
match(s, c(2,4))
95+
96+
The :meth:`~pandas.core.groupby.GroupBy.apply` method can be used to replicate
97+
this:
98+
99+
.. ipython:: python
100+
101+
s = pd.Series(np.arange(5),index=np.arange(5)[::-1],dtype=np.float32)
102+
s.apply(lambda x: [2, 4].index(x) if x in [2,4] else np.nan)
103+
104+
For more details and examples see :ref:`the reshaping documentation
105+
<indexing.basics.indexing_isin>`.
106+
69107
|tapply|_
70108
~~~~~~~~~
71109

@@ -372,6 +410,9 @@ For more details and examples see :ref:`the reshaping documentation
372410
.. |aggregate| replace:: ``aggregate``
373411
.. _aggregate: http://finzi.psych.upenn.edu/R/library/stats/html/aggregate.html
374412

413+
.. |match| replace:: ``match`` / ``%in%``
414+
.. _match: http://finzi.psych.upenn.edu/R/library/base/html/match.html
415+
375416
.. |tapply| replace:: ``tapply``
376417
.. _tapply: http://finzi.psych.upenn.edu/R/library/base/html/tapply.html
377418

0 commit comments

Comments
 (0)