4

I am learning python and am just getting into pandas. I am already familiar with R. I would like to be able to create a data frame in python and then somehow get it into R.

Could I do this somehow by writing the file to a CSV and then opening it into R? If so, where should I go to learn how to do this?

I found this website: http://pandas.pydata.org/pandas-docs/dev/r_interface.html that seems to be one solution - it uses rpy2 as a link between python and R. It is unclear to me, though, how the data frame actually gets opened up in R.

But I couldn't get that far because I had an error when trying to execute this code:

import pandas.rpy.common as com

I get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/pandas/rpy/__init__.py", line 2, in <module>
    from common import importr, r, load_data
  File "/Library/Python/2.7/site-packages/pandas/rpy/common.py", line 12, in <module>
    from rpy2.robjects.packages import importr
  File "rpy2.py", line 1, in <module>
    import pandas.rpy.common as com
AttributeError: 'module' object has no attribute 'rpy'

I installed rpy2 using easy_install. It seems to be the latest version.

What should I do?

bill999
  • 2,147
  • 8
  • 51
  • 103
  • 3
    I think this specific error you're getting is caused by you naming your file `rpy2`, which stops python being able to see the actual `rpy2` module. As for your other questions, I think you should stick with using csv files to bridge between R and Python for the moment, it saves you having to deal with the quirks of two languages simultaneously. – Marius Sep 21 '13 at 06:23
  • 3
    Since you are already familiar with R, communication between R data frames and Python pandas' DataFrame does not have to go through flat files, even when only learning. iPython's Rmagic is already able to perform an automagic conversion between the two in a number of situations, and might be a good way to get familiar with Python. – lgautier Sep 21 '13 at 13:21
  • 1
    i usually find that messing around with r and python together is only worth it when i have a pretty clear series of tasks i know i'll want to perform time and time again. so for exploring things -- as unsexy as it is -- i just df.to_csv() then read the file in R. – badgley Apr 17 '14 at 19:21

0 Answers0