python - 使用 rpy2 将 R 对象转换为 Python 对象

标签 python r rpy2

问题:如何将 R 对象转换为 python 对象

我的情况:我需要将 cor.test() 的结果用于 python 例程。

correlation = robjects.r('function(x, y) cor.test(x, y)')
corr= correlation(robjects.IntVector(goodtotemp), robjects.IntVector(goodGDPs))
print corr

print corr[3]
print 'coef:',type(corr[3])

如预期的输出:

     cor 
0.984881 
coef: <class 'rpy2.robjects.vectors.FloatVector'>

但是,我不能将 corr[3] 用作 python 对象,

c=corr[3]
print 'c:',c*10., type(c)

看(这是我如何知道我做错了什么!),输出:

c:
Traceback (most recent call last):
  File "./GDPAnalyzer.py", line 234, in <module>
    print 'c:',c*10., type(c)
TypeError: unsupported operand type(s) for *: 'FloatVector' and 'float'

感谢任何提示/帮助!

最佳答案

段错误应该被报告为 rpy2 错误。否则,请尝试:

c.ro * 3

或:

c[0] * 3

关于python - 使用 rpy2 将 R 对象转换为 Python 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5652855/

相关文章:

python - 使用 python w/rest api 与 Azure Key Vault 交互

python - glibc 检测到 *** python : double free or corruption (! prev) SWIG

python - 子进程 check_output OSError : [Errno 2] No such file or directory

r - 将多个时区转换为一个 - r

FindCorrelation 中需要 R、TRUE/FALSE

python - 多处理:同时附加到 2 个列表

r - Purrr 的修改函数

python - 使用 rpy2 将 R 包安装/导入到 python 中,导入/忽略有问题的包

python - 从 Python rpy2 使用命令行参数调用 R 脚本

python - rpy2:如何从调用 R 函数中获取返回值