diff --git a/doc/source/comparison_with_r.rst b/doc/source/comparison_with_r.rst index 0738930b4ea25..a5b8fabac11ac 100644 --- a/doc/source/comparison_with_r.rst +++ b/doc/source/comparison_with_r.rst @@ -66,6 +66,44 @@ function. For more details and examples see :ref:`the groupby documentation `. +|match|_ +~~~~~~~~~~~~ + +A common way to select data in R is using ``%in%`` which is defined using the +function ``match``. The operator ``%in%`` is used to return a logical vector +indicating if there is a match or not: + +.. code-block:: r + + s <- 0:4 + s %in% c(2,4) + +The :meth:`~pandas.DataFrame.isin` method is similar to R ``%in%`` operator: + +.. ipython:: python + + s = pd.Series(np.arange(5),dtype=np.float32) + s.isin([2, 4]) + +The ``match`` function returns a vector of the positions of matches +of its first argument in its second: + +.. code-block:: r + + s <- 0:4 + match(s, c(2,4)) + +The :meth:`~pandas.core.groupby.GroupBy.apply` method can be used to replicate +this: + +.. ipython:: python + + s = pd.Series(np.arange(5),dtype=np.float32) + Series(pd.match(s,[2,4],np.nan)) + +For more details and examples see :ref:`the reshaping documentation +`. + |tapply|_ ~~~~~~~~~ @@ -372,6 +410,9 @@ For more details and examples see :ref:`the reshaping documentation .. |aggregate| replace:: ``aggregate`` .. _aggregate: http://finzi.psych.upenn.edu/R/library/stats/html/aggregate.html +.. |match| replace:: ``match`` / ``%in%`` +.. _match: http://finzi.psych.upenn.edu/R/library/base/html/match.html + .. |tapply| replace:: ``tapply`` .. _tapply: http://finzi.psych.upenn.edu/R/library/base/html/tapply.html