Translating named lists from R to rpy2 in Python?

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

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

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

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

Is this a robject.ListVector ? I tried:

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

but this is not entirely true, because I'm not sure how to tell the ListVector object that the name of this object is Var1 , that is, something like list(Var1 = Var1) .

How can this be correctly translated to rpy2?

+4
source share
2 answers

After many hours of trial and error, I found a solution. I don’t quite understand why such variants of this did not work or why this scheme is not interchangeable with data files, but I should work:

 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) 
+1
source

If I understand your question correctly, 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 details.

+3
source

Source: https://habr.com/ru/post/1494855/


All Articles