Previous: Enhanced Robj, Up: Useful examples


4.3.2 DataFrame class

This example is a subclass of the Enhanced Robj (see Enhanced Robj), which can be used to mimic the `Data Frame' class of the R language.

It overrides the __getattr__ method for retrieving the columns of the data frame object. It adds a method for accessing the rows and it inherits the representation and as_r method.

File dataframe.py:

     from rpy import *
     import erobj
     
     class DataFrame(erobj.ERobj):
         def __init__(self, robj):
             erobj.ERobj.__init__(self, robj)
     
         def rows(self):
             return r.attr(self.robj, 'row.names')
     
         def __getattr__(self, attr):
             o = self.__dict__['robj']
             if attr in as_list(r.colnames(o)):
                 return r['$'](o, attr)
             return self.__dict__[attr]

An example of use:

     >>> from rpy import *
     >>> from dataframe import *
     >>> class_table['data.frame'] = DataFrame
     >>> set_default_mode(CLASS_CONVERSION)
     >>>
     >>> e = r.as_data_frame({'foo': [4,5,6], 'bar': ['X','Y','Z']})
     >>> e
     <dataframe.DataFrame instance at 0x8156e34>
     >>> print e
       foo bar
     1   4   X
     2   5   Y
     3   6   Z
     >>>
     >>> e.foo
     [4, 5, 6]
     >>> e.bar
     ['X', 'Y', 'Z']
     >>> e.rows()
     ['1', '2', '3']