Question

What is the equivalent of the following R code in Rpy2 in python?

Var1 = c("navy", "darkgreen")
names(Var1) = c("Class1", "Class2")
ann_colors = list(Var1 = Var1)

It's not clear what ann_colors is. When evaluated in R it gives:

> ann_colors
$Var1
     Class1      Class2 
     "navy" "darkgreen" 

Is it an robject.ListVector? I tried:

robjects.ListVector({"Class1": "navy", "Class2": "green"})

but it's not quite it because I'm not sure how to tell the ListVector object that the name of this object is Var1, i.e. something like list(Var1 = Var1).

How can this be properly translated to rpy2?

Was it helpful?

Solution 2

After many hours of trial and error, I found the solution. I don't fully understand why the similar variants of this did not work, or why this scheme is not interchangeable with dataframes, but what I got to work is:

anno_colors = robj.StrVector(tuple(["navy", "green"]))
anno_colors.names = ["Class1", "Class2"]
od = OrderedDict()
od["Type"] = anno_colors
result = robj.ListVector(od)
print str(result)

OTHER TIPS

If I understand right your question what you are looking for is a TaggedList:

import rpy2.rlike.container as rlc
Var1 = rlc.TaggedList(["navy","darkgreen"], tags=('Class1', 'Class2'))

See http://rpy.sourceforge.net/rpy2/doc-2.2/html/rlike.html for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top