Reputation: 4052
I have an .RData
file in the system which contains three objects - all R-DataFrame
s. I would like to load them into python as pandas DataFrames. The problem I get is, that the objects are not loaded via rpy
, only their names:
from rpy2.robjects import r
from rpy2.robjects import pandas2ri
import pandas as pd
file="./normalization.RData"
rf = r.load(file)
rf
<StrVector - Python:0x7fdf1a28cb08 / R:0x4ce82c8>
['df1', 'df2', 'df3']
f1[2]
'annodf'
In [11]:
Either only the names are loaded, or I do not understand how to access/convert the DFs from the RData.
Does somebody have the solution to this?
Upvotes: 1
Views: 4478
Reputation: 81
For those who get below error:
AttributeError: module 'rpy2.robjects.pandas2ri' has no attribute 'ri2py_dataframe'
change this:
df2 = pandas2ri.ri2py_dataframe(r['df2'])
to this:
df2 = rpy2.robjects.conversion.rpy2py(r['df2'])
Used R version: 3.6.3
Upvotes: 0
Reputation: 4052
Ok - I just realized that the object are not really loaded into rf
variable, but into the rpy2.rojbects.r
which represents the R-environment.. that is tricky!
Therefore the following works:
import pandas as pd
from rpy2.robjects import r
import rpy2.robjects.pandas2ri as pandas2ri
#load into the env
file="./normalization.RData"
rf=r['load'](file)
rf
<StrVector - Python:0x7fdf1a28cb08 / R:0x4ce82c8>
['df1', 'df2', 'df3']
#acces file in env and convert
df2=pandas2ri.ri2py_dataframe(r['df2'])
type(df2)
pandas.core.frame.DataFrame
Upvotes: 3