rpy2: Python interface to the R language

3.6.7 · active · verified Wed Apr 15

rpy2 is a powerful Python package that provides a bridge between Python and the R programming language. It enables seamless integration of R's statistical capabilities and specialized packages with Python's versatile ecosystem. The library is actively developed, with its current stable version being 3.6.7, and maintains a continuous development and release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize rpy2, activate automatic conversion between pandas DataFrames and R data.frames, execute arbitrary R code, access R objects from Python, and call R functions with Python data structures. It showcases the core `rpy2.robjects` and `rpy2.robjects.pandas2ri` modules.

import rpy2.robjects as ro
from rpy2.robjects.packages import importr
from rpy2.robjects import pandas2ri
import pandas as pd

# Activate conversion between pandas and R (optional, but common)
pandas2ri.activate()

# Import an R package (e.g., base R's 'stats' for statistical functions)
stats = importr('stats')
base = importr('base')

# Define R code as a Python string and execute it
ro.r('''
    # Generate a sequence of numbers
    x <- 1:10
    # Calculate the mean
    mean_x <- mean(x)
    # Create an R data frame
    r_df <- data.frame(A=c(1,2,3), B=c('x','y','z'))
''')

# Access R variables from Python
mean_x = ro.r['mean_x'][0]
print(f"Mean of x from R: {mean_x}")

# Get an R data frame and convert it to pandas (if pandas2ri is activated)
r_dataframe = ro.r['r_df']
py_dataframe = pandas2ri.rpy2py(r_dataframe)
print("Python DataFrame from R:")
print(py_dataframe)
print(f"Type of py_dataframe: {type(py_dataframe)}")

# Example: Call an R function with Python objects
py_data = pd.DataFrame({'value': [10, 20, 30], 'group': ['A', 'B', 'A']})
r_data = pandas2ri.py2rpy(py_data)

# Use an R function (e.g., 't.test') from the 'stats' package
t_test_result = stats.t_test(ro.Formula('value ~ group'), data=r_data)
print("\nT-test result from R:")
print(t_test_result)

view raw JSON →