I'm not completely sure about this, it looks like that R has fortran-style arrays (left-most index changes fastest), while in Python we have c-style arrays ...
A solution would be to reshape the array in 'F'ortran order:
>>> arr = conn.r("matrix(1:12, nrow=4, ncol=3)")
>>> arr.reshape((4, 3), order='F')
array([[ 1, 5, 9],
[ 2, 6, 10],
[3, 7, 11],
[4, 8, 12]])
Not sure yet whether this should be a default transformation in pyRserve ... any comments about this?
Ralph
Am Montag, 18. Juni 2012 22:14:58 UTC+2 schrieb Vebjorn Ljosa:
I think matrix elements are being returned in the wrong order.
In R, I get the following:
> matrix(1:12, nrow=4, ncol=3)
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
When transfering the matrix to Python and pyRserve, the values are filled in row-wise instead of column-wise:
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyRserve
>>> pyRserve.__version__
'0.5.2'
>>> conn = pyRserve.connect()
>>> print conn.r("matrix(1:12, nrow=4, ncol=3)")
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
>>>
Thanks,
Vebjorn