Question

a and b are both rpy2 IntVectors:

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

How can I calculate the b - a difference? I want the result as an IntVector.

Was it helpful?

Solution

Try using the 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]

OTHER TIPS

subtract  = r('''function(x, y) x - y''')
subtract(b, a)

The good about this solution is that it can not only handle IntVectors, but any R type. The bad is that passing commands as strings to the R interpreter is ugly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top