rpy2 / R interface¶
Note
This is all highly experimental. I would like to get more people involved with building a nice RPy2 interface for pandas
If your computer has R and rpy2 (> 2.2) installed (which will be left to the reader), you will be able to leverage the below functionality. On Windows, doing this is quite an ordeal at the moment, but users on Unix-like systems should find it quite easy. rpy2 evolves in time, and is currently reaching its release 2.3, while the current interface is designed for the 2.2.x series. We recommend to use 2.2.x over other series unless you are prepared to fix parts of the code, yet the rpy2-2.3.0 introduces improvements such as a better R-Python bridge memory management layer so it might be a good idea to bite the bullet and submit patches for the few minor differences that need to be fixed.
# if installing for the first time
hg clone http://bitbucket.org/lgautier/rpy2
cd rpy2
hg pull
hg update version_2.2.x
sudo python setup.py install
Note
To use R packages with this interface, you will need to install them inside R yourself. At the moment it cannot install them for you.
Once you have done installed R and rpy2, you should be able to import pandas.rpy.common without a hitch.
Transferring R data sets into Python¶
The load_data function retrieves an R data set and converts it to the appropriate pandas object (most likely a DataFrame):
In [1]: import pandas.rpy.common as com
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-63ebebefea80> in <module>()
----> 1 import pandas.rpy.common as com
/Users/jtratner/projects/python/pandas/pandas/rpy/common.py in <module>()
12 import pandas.util.testing as _test
13
---> 14 from rpy2.robjects.packages import importr
15 from rpy2.robjects import r
16 import rpy2.robjects as robj
ImportError: No module named rpy2.robjects.packages
In [2]: infert = com.load_data('infert')
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-cc2737d56097> in <module>()
----> 1 infert = com.load_data('infert')
NameError: name 'com' is not defined
In [3]: infert.head()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-3-bd9b2175a530> in <module>()
----> 1 infert.head()
NameError: name 'infert' is not defined
Converting DataFrames into R objects¶
New in version 0.8.
Starting from pandas 0.8, there is experimental support to convert DataFrames into the equivalent R object (that is, data.frame):
In [4]: from pandas import DataFrame
In [5]: df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]},
...: index=["one", "two", "three"])
...:
In [6]: r_dataframe = com.convert_to_r_dataframe(df)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-6-15b2e27d2d1a> in <module>()
----> 1 r_dataframe = com.convert_to_r_dataframe(df)
NameError: name 'com' is not defined
In [7]: print(type(r_dataframe))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-7-fff2d2942819> in <module>()
----> 1 print(type(r_dataframe))
NameError: name 'r_dataframe' is not defined
In [8]: print(r_dataframe)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-8-c144b8f4c119> in <module>()
----> 1 print(r_dataframe)
NameError: name 'r_dataframe' is not defined
The DataFrame’s index is stored as the rownames attribute of the data.frame instance.
You can also use convert_to_r_matrix to obtain a Matrix instance, but bear in mind that it will only work with homogeneously-typed DataFrames (as R matrices bear no information on the data type):
In [9]: r_matrix = com.convert_to_r_matrix(df)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-9-2676d8f58ade> in <module>()
----> 1 r_matrix = com.convert_to_r_matrix(df)
NameError: name 'com' is not defined
In [10]: print(type(r_matrix))
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-10-2376079ff3d0> in <module>()
----> 1 print(type(r_matrix))
NameError: name 'r_matrix' is not defined
In [11]: print(r_matrix)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-e2b90f93ebe7> in <module>()
----> 1 print(r_matrix)
NameError: name 'r_matrix' is not defined