Last active
April 8, 2022 14:06
-
-
Save LeiG/8094753a6cc7907c716f to your computer and use it in GitHub Desktop.
Python and .RData files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import rpy2.robjects as robjects | |
import pandas.rpy.common as com | |
import pandas as pd | |
## load .RData and converts to pd.DataFrame | |
robj = robjects.r.load('test.RData') | |
# iterate over datasets the file | |
for sets in robj: | |
myRData = com.load_data(sets) | |
# convert to DataFrame | |
if not isinstance(myRData, pd.DataFrame): | |
myRData = pd.DataFrame(myRData) | |
## save pd.DataFrame to R dataframe | |
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C':[7,8,9]}, | |
index=["one", "two", "three"]) | |
r_dataframe = com.convert_to_r_dataframe(df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As an alternative for those who would prefer not having to install R in order to accomplish this task (r2py requires it), there is a new package "pyreadr" which allows reading RData and Rds files directly into python without dependencies.
It is a wrapper around the C library librdata, so it is very fast.
You can install it easily with pip:
As an example you would do:
The repo is here: https://github.com/ofajardo/pyreadr