Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rpy2: Convert FloatVector or Matrix back to a Python array or list?

Tags:

python

rpy2

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!

like image 996
Néstor Avatar asked Aug 02 '12 00:08

Néstor


3 Answers

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

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

like image 146
Jeremy Z Avatar answered Sep 27 '22 22:09

Jeremy Z


This worked like a charm:

vector=numpy.asarray(vector_R)
like image 28
AnirudhJ Avatar answered Sep 27 '22 22:09

AnirudhJ


import rpy2.robjects.numpy2ri as rpyn
vector=rpyn.ri2py(v)

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

like image 29
Mazi WIzi Avatar answered Sep 27 '22 23:09

Mazi WIzi