Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas.rpy.common.load_data() usage/documentation?

I am trying to convert some <class 'rpy2.robjects.vectors.Matrix'> variables into Pandas dataframes.

There is a lot of copy-paste instructions of how to do so on the internet, all giving the brief example:

pandas.rpy.common.load_data("infert")

without any information on where "infert" is coming from. I was unable to get any sort of documentation on this function (why is there none?), but apparently I cannot use it:

summary= r.summary(linear_model)
filtered_summary=summary.rx2("tTable")
print com.load_data("filtered_summary")

gives me:

---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-68-a087eddd5220> in <module>()
      8 #print test1_sum.names
      9 print type(r_res)
---> 10 print com.load_data("filtered_summary")
     11 #print pd.DataFrame(test1_sum.rx2("tTable"))
     12 

/usr/lib64/python2.7/site-packages/pandas/rpy/common.pyc in load_data(name, package, convert)
     29     r.data(name)
     30 
---> 31     robj = r[name]
     32 
     33     if convert:

/usr/lib64/python2.7/site-packages/rpy2/robjects/__init__.pyc in __getitem__(self, item)
    226 
    227     def __getitem__(self, item):
--> 228         res = _globalenv.get(item)
    229         res = conversion.ri2ro(res)
    230         res.__rname__ = item

LookupError: 'filtered_summary' not found

while:

summary= r.summary(linear_model)
print com.load_data("summary")

gives me:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-69-b51722281aa4> in <module>()
      8 #print test1_sum.names
      9 print type(r_res)
---> 10 print com.load_data("summary")
     11 #print pd.DataFrame(test1_sum.rx2("tTable"))
     12 

/usr/lib64/python2.7/site-packages/pandas/rpy/common.pyc in load_data(name, package, convert)
     32 
     33     if convert:
---> 34         return convert_robj(robj)
     35     else:
     36         return robj

/usr/lib64/python2.7/site-packages/pandas/rpy/common.pyc in convert_robj(obj, use_pandas)
    222             return converter(obj)
    223 
--> 224     raise TypeError('Do not know what to do with %s object' % type(obj))
    225 
    226 

TypeError: Do not know what to do with <class 'rpy2.robjects.functions.SignatureTranslatedFunction'> object

So:

  • How do I use load_data correctly
  • How can I best get my R matrix converted to a Pandas DataFrame?
like image 295
TheChymera Avatar asked Jun 10 '26 15:06

TheChymera


1 Answers

I don't know whether this is the "correct" use of load_data but I've found that if your R dataframe (say, myRData) is stored in the default workspace (.RData) in the default working directory then you can use load_data to load myRData using:

import rpy2.robjects as robjects
import pandas.rpy.common as com
print robjects.r.load(".RData")
myRData = com.load_data('myRData')

You could use robjects.r.XXX to run other R functions such as robjects.r.getwd() or robjects.r.setwd("path_to_new_working_directory") to navigate to new working directories.

like image 72
user1718097 Avatar answered Jun 13 '26 05:06

user1718097