python - 将命名列表向量从 R 转换为 Python 中的 rpy2?

标签 python r rpy2

在 python 中的 Rpy2 中,以下 R 代码等效于什么?

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

不清楚ann_colors 是什么。当在 R 中评估时,它给出:

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

它是一个robject.ListVector吗?我试过:

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

但这并不完全是因为我不确定如何告诉 ListVector 对象该对象的名称是 Var1,即类似于 list(Var1 = Var1).

如何将其正确翻译成 rpy2?

最佳答案

如果我对你的问题的理解是正确的,那么你正在寻找的是一个 TaggedList:

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

参见 http://rpy.sourceforge.net/rpy2/doc-2.2/html/rlike.html了解更多详情。

关于python - 将命名列表向量从 R 转换为 Python 中的 rpy2?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006542/

相关文章:

r - 通过另一个函数将数据和列名传递给 ggplot

python - 使用 rpy.robjects 时出现一条消息 "Unable to determine R home: [WinError 2] The system could not find the specified file"

python - 客户端从tornado网络服务器下载完成后如何调用函数?

Python Pandas,读取文件并跳过标题前的行

r - r 中的 library()/require() 和 source() 有什么区别?

r - ipython notebook R cell magic作为python函数

python - 通过rpy2从ggplot2中的geom_histogram中删除基线?

python - 根据像素值比较多个图像

python - 有没有好方法在 numpy 数组上进行 "moving"计算?

r - ggplot 抖动 geom_errorbar?