[Rpy] Addition to R vectors not behaving as expected (as in R)

2013-03-21 Thread Luca Beltrame
This occurs with the latest 2.3. Example case:

robjects.IntVector([0,1,2]) + 1

This is what it returns

IntVector - Python:0x7fadd7a7a320 / R:0xb385b18
[   0,1,2,1]

While I'd expect, like in R

 c(0,1,2) + 1
[1] 1 2 3

Is this a bug, or do I have to do things differently?

-- 
Luca Beltrame, Ph.D. - Translational Genomics Unit
Oncology Department, Mario Negri Institute


signature.asc
Description: This is a digitally signed message part.
--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar___
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list


Re: [Rpy] Addition to R vectors not behaving as expected (as in R)

2013-03-21 Thread Laurent Gautier

On 2013-03-21 02:59, Luca Beltrame wrote:

This occurs with the latest 2.3. Example case:

robjects.IntVector([0,1,2]) + 1

This is what it returns

IntVector - Python:0x7fadd7a7a320 / R:0xb385b18
[   0,1,2,1]

While I'd expect, like in R


c(0,1,2) + 1

[1] 1 2 3

Is this a bug, or do I have to do things differently?



The idea was that a vector would behave like a Python sequence. For example

 x = [1,2]
 x + [3, ]
[1,2,3]
Now where I met ambiguity bridging the two worlds (Python and R) is that 
there is no such thing as a scalar in R (they are vectors of length 1).

In Python this is an error.
 x+3
Traceback (most recent call last):
  File stdin, line 1, in module
TypeError: can only concatenate list (not int) to list

Since I am converting Python scalars into vectors whenever passing them 
to R I must have thought that this should be the same for when
__add__ is used. Now I see that in the current robjects model, 
`robjects.IntVector([0,1,2]) + 1` should raise a type Error.


To use R-style operation, I solved this by adding a delegating attribute 
called `ro` (R operator).

For example here:
robjects.IntVector([0,1,2]).ro + 1
(documentation is here:
http://rpy.sourceforge.net/rpy2/doc-2.3/html/vector.html#operators
).

L.




--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar


___
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list


--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar___
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list


Re: [Rpy] Addition to R vectors not behaving as expected (as in R)

2013-03-21 Thread Thomas Kluyver
On 21 March 2013 14:28, Laurent Gautier lgaut...@gmail.com wrote:

 In Python this is an error.
  x+3
 Traceback (most recent call last):
   File stdin, line 1, in module
 TypeError: can only concatenate list (not int) to list


With a list, yes. With a numpy array, which is probably as relevant for
many rpy2 users, adding an integer adds it to every element:

In [4]: x + 1
Out[4]: array([1, 2, 3])

Thomas
--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar___
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list


Re: [Rpy] Addition to R vectors not behaving as expected (as in R)

2013-03-21 Thread Luca Beltrame
In data giovedì 21 marzo 2013 15:38:22, Thomas Kluyver ha scritto:

 With a list, yes. With a numpy array, which is probably as relevant for
 many rpy2 users, adding an integer adds it to every element:

That was actually the reason I expected it to work. However it's not a huge 
issue to use .ro (I do in several other places for selection).

– 
Luca Beltrame, Ph.D. - Translational Genomics Unit
Oncology Department, Mario Negri Institute


signature.asc
Description: This is a digitally signed message part.
--
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_mar___
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list