在 Python 3 的 rpy2 中,向量和标量的逐元素比较失败

Element-wise comparison of vector and scalar fails in rpy2 with Python 3

看起来 rpy2 在 Python3 和 Python2 中的行为并不完全相同。特别是,在 Python 2.7.x 中,我可以将 Vector 与标量进行比较,而在 Python 3.5.x 中,如果我尝试为此,会出现以下错误:

TypeError: unorderable types: IntVector() > int()

这是我的代码片段:

>>> from rpy2 import robjects
>>> x = robjects.IntVector([5, -1])
>>> x > 0

这是我的 pip freeze 的摘录(在两种环境中都相同):

rpy2==2.8.2
numpy==1.11.1

我想知道它是 rpy2 中的错误还是 rpy2 所依赖的某些库(例如 numpy)的不同行为。

如果有错误,我认为是 Python 2.

R 向量是一个序列,默认的 Python 行为无法进行此类比较。例如:

>>> [1,2,3] < 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-f7a2f4fe21e0> in <module>()
----> 1 [1,2,3] < 2

TypeError: unorderable types: list() < int()

另一方面,R 的向量化对象可以通过委托器 .ro 调用(如 "R Operator")。参见 https://rpy2.readthedocs.io/en/version_2.8.x/vector.html#operators

>>> v = IntVector([1,2,3])
>>> v.ro < 2 
R object with classes: ('logical',) mapped to:
<BoolVector - Python:0x7fc7789a5a08 / R:0x22d25f8>
[       1,        0,        0]