From rpy2 docs , R-matrices are just vectors with their set of dim attributes. So, for a two-dimensional two-dimensional array x
import rpy2.robjects as robj
nr, nc = x.shape
xvec = robj.FloatVector(x.transpose().reshape((x.size))
xr = robj.r.matrix(xvec, nrow=nr, ncol=nc)
You need to transpose the numpy array, because R fills the matrices in columns.
Edit: Actually, you could just set byrow = True in the matrix function R, and then you would not need to transpose.
source
share