将命名列表向量从 R 翻译成 Python 中的 rpy2? [英] translating named list vectors from R into rpy2 in Python?
问题描述
python 中 Rpy2 中以下 R 代码的等价物是什么?
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)
不清楚 ann_colors
是什么.在 R 中评估时,它给出:
It's not clear what ann_colors
is. When evaluated in R it gives:
> ann_colors
$Var1
Class1 Class2
"navy" "darkgreen"
它是一个 robject.ListVector
吗?我试过了:
Is it an robject.ListVector
? I tried:
robjects.ListVector({"Class1": "navy", "Class2": "green"})
但不完全是因为我不知道如何告诉 ListVector 对象这个对象的名称是 Var1
,即类似于 list(Var1 = Var1)代码>.
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)
.
如何将其正确转换为 rpy2?
How can this be properly translated to rpy2?
推荐答案
经过数小时的反复试验,我找到了解决方案.我不完全理解为什么类似的变体不起作用,或者为什么这个方案不能与数据帧互换,但我要做的是:
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)
这篇关于将命名列表向量从 R 翻译成 Python 中的 rpy2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!