Rmagic, A Handy Interface Bridging Python and R

Originally posted on Yet Another Blog in Statistical Computing:

Rmagic (http://ipython.org/ipython-doc/dev/config/extensions/rmagic.html) is the ipython extension that utilizes rpy2 in the back-end and provides a convenient interface accessing R from ipython. Compared with the generic use of rpy2, the rmagic extension allows users to exchange objects between ipython and R in a more flexible way and to run a single R function or a block of R code conveniently.

Below is an example demonstrating a simple use case how to push a pandas DataFrame object into R, convert it to a R data.frame, and then transfer back to a new pandas DataFrame object again.
1
In [1]: import pandas as pd

In [2]: # READ DATA INTO PANDAS DATAFRAME

In [3]: pydf1 = pd.read_table(‘../data/csdata.txt’, header = 0)

In [4]: print pydf1.describe()
LEV_LT3 TAX_NDEB COLLAT1 SIZE1 PROF2 \
count 4421.000000 4421.000000 4421.000000 4421.000000 4421.000000
mean 0.090832 0.824537 0.317354 13.510870 0.144593
std 0.193872 2.884129 0.227150 1.692520 0.110908
min 0.000000 0.000000 0.000000 7.738052 0.000016

View original 476 more words

About these ads

About dmvianna

Daniel is a keen reader of history and all things political. In his free time, Daniel has also dedicated his mind to study the brain in a laboratory setting. Part of his knowledge of history comes from listening to podcasts while he counts brain cells in a microscope. True story.
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Rmagic, A Handy Interface Bridging Python and R

  1. laviniag says:

    Hi Daniel, this comment is actually in relation to a Dec 6th post on “speed up map plotting” but wordpress wouldn’t let me comment there so I am trying here:

    “Very nice post. Could you put a link to the actual census data that you used?, I am trying to do something similar and am parsing something incorrectly to ggplot but am not sure what! Thanks.

    • dmvianna says:

      Hi, Lavinia.

      Indeed, my initial intention was indeed to post a link, but ABS requires users to register before giving them access to the census data. Registering is free. The data I used was from their data pack product. I downloaded the topmost leftmost option in their datapacks download page (which you see after loggin in), and after unzipping it I used the exact directory structure and filenames as provided, so the example would be reproducible.

      I hope these instructions will be enough for you to reproduce my example, let me know if you need more help.

      Cheers, Daniel.

      P.S: The URL for the Data Packs is below, WordPress is not rendering HTML properly in my comment:

      http://www.abs.gov.au/websitedbs/censushome.nsf/home/datapacks?opendocument&navpos=250

  2. Pingback: Rmagic, A Handy Interface Bridging Python and R - R Project Aggregate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s