【问题标题】:Difference of two IntVectors in RpyRpy中两个IntVectors的差异
【发布时间】:2013-05-07 17:42:51
【问题描述】:

ab 都是 rpy2 IntVectors:

<IntVector - Python:0x10676dfc8 / R:0x7fc714d64948>
[      81, NA_integer_, NA_integer_, ...,      120,       46, NA_integer_]

如何计算b - a 的差异?我希望结果为IntVector

【问题讨论】:

    标签: rpy2 subtraction


    【解决方案1】:

    尝试使用R operator attribute .ro

    In [1]: from rpy2 import robjects
    
    In [2]: x = robjects.IntVector(range(10))
    
    In [3]: y = robjects.IntVector(range(10))
    
    In [4]: x.ro-y
    Out[4]: 
    <IntVector - Python:0x1067d3830 / R:0x102d6ef20>
    [       0,        0,        0, ...,        0,        0,        0]
    
    In [5]: x.ro+y
    Out[5]: 
    <IntVector - Python:0x1067d3cf8 / R:0x102d6eec8>
    [       0,        2,        4, ...,       14,       16,       18]
    

    【讨论】:

      【解决方案2】:
      subtract  = r('''function(x, y) x - y''')
      subtract(b, a)
      

      这个解决方案的好处是它不仅可以处理 IntVectors,还可以处理任何 R 类型。 不好的是,将命令作为字符串传递给 R 解释器很难看。

      【讨论】:

        猜你喜欢
        • 2016-07-31
        • 1970-01-01
        • 2011-06-10
        • 2023-03-12
        • 2011-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多