RPy and the R statistical package
The R statistical package is one of the most commonly used ones for analysing statistical data. It has its own language. There is a Python wrapper around it called RPy.2 The main reason to use RPy would be if you have lots of existing R code that you wish to interface to in Python.
There are a few things to keep in mind when using RPy. Standard Python collection types or NumPy arrays have to be converted into special RPy data types, and results that are returned from R have to be suitably interpreted. Reading the R documentation is crucial to using RPy.
We will illustrate the use of R via RPy for a few standard examples.
Binomial test
First we consider the binomial test, which is concerned with the number of occurrences of an event that has a fixed probability of occurring, given a certain number of trials. R has a method, ‘binom.test’, to do the binomial test. We create a function, binomialTailTest(), which calls this method via RPy, and which has the same arguments as in our previous version of the function in Chapter 22, which used SciPy.
First we need to import the RPy module, rpy2.robjects, which we call R below. This has an object inside it, R.r, which is what we use to get hold of R methods, using dictionary syntax keyed on the name of the R method. Here we want to use the R method binom.test, and so R.r['binom.test'] is the Python version of this R method.