It's a good question.

Let's take a self-contained example:

robjects.r('''
dataf <- data.frame(x=sample(c("a", "b"), 6, replace=TRUE),
                     y=1:6)''')
tt = robjects.r('dataf[[1]]')

Now tt is reflecting an R object of type "factor", which is as you know 
most a vector of integers with an associated vector of "levels".

When iterating on the elements with

[t for t in tt]

the elements of the vector are iterated through, that is the integers, 
and returned in a list.

Yet, it is possible to interpolate on the flyt the integers into their 
levels (note the adjustment for one-offset indexing in R):

[tt.do_slot("levels")[t-1] for t in tt]


Now you can define a class RFactor, for example like this:

class RFactor(robjects.RVector):
   def level_gen(self):
     return (self.do_slot("levels")[elt-1] for elt in self)
   def levels(self):
     return [elt for elt in self.do_slot("levels")]

ftt = RFactor(tt)

list(ftt.level_gen())


The obvious question is then: why isn't a class RFactor already in 
robjects ? The answer is: I was not quite sure about the best way to 
have it, and tools for implementing one's own are available.

Automatic conversion to one's own RFactor class can be added at little 
cost. (See for example
http://rpy.sourceforge.net/rpy2/doc/html/robjects_convert.html
)


L.


Jeff Gentry wrote:
> Another one that I'm figuring is operator error on my part, but the last
> one wasn't so who knows :)
> 
> I've been dealing with some data.frames w/ no real problem until now,
> where everything seems to be getting factorized (ie 0,1,2,etc instead of
> 'abc', 'def', 'ghi') coming across the linkage, presumably the difference
> here being that the columns on this data.frame happens to be a factor
> while the columns on the other ones I'm guessing were not.  Laurent, for
> context, this is a pData data.frame from an AnnotatedDataFrame.  Note that
> I can get this working by adding an 'as.character' here, but I just wanted
> to bring this up in case I'm doing something wrong and/or this isn't
> intended behavior.
> 
> Example:
> tt = robjects.r('pData(pheno)[,4]')
>>>> [t for t in tt]
> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> 1, 1, 1, 1, 1, 1]
> 
>>>> robjects.r('print(pData(pheno)[1,4])')
> [1]
>  Test
> 
> 
> 
> Levels: 
> 
> Test
> 
> 
> 
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by:
> SourcForge Community
> SourceForge wants to tell your story.
> http://p.sf.net/sfu/sf-spreadtheword
> _______________________________________________
> rpy-list mailing list
> rpy-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rpy-list


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to