9

I'm using rpy2 and I have this issue that's bugging me: I know how to convert a Python array or list to a FloatVector that R (thanks to rpy2) can handle within Python, but I don't know if the opposite can be done, say, I have a FloatVector or Matrix that R can handle and convert it back to a Python array or list...can this be done?

Thanks in advance!

4

4 回答 4

12

This worked like a charm:

vector=numpy.asarray(vector_R)
于 2016-05-03T09:56:07.880 回答
4

Found the answer myself :-). Suppose vector_R is a FloatVector. To convert it back to Python you need to do:

import rpy2.robjects.numpy2ri as rpyn
vector=rpyn.ri2numpy(vector_R)

And that's it! "vector" is now a Numpy array.

于 2012-08-02T00:57:53.283 回答
3

In the latest version of rpy2, you can simply do this in a direct way:

import numpy as np array=np.array(vector_R)

于 2018-09-19T06:42:07.443 回答
1
import rpy2.robjects.numpy2ri as rpyn
vector=rpyn.ri2py(v)

Does it in the new rpy2, where v is the vector

于 2019-04-27T16:02:57.983 回答