Convert R matrix to Pandas Dataframe

I am trying to convert a matrix R to a pandas framework. I use:

import pandas.rpy.common as com
df = com.convert_to_r_dataframe(r_matrix)

And I get:

TypeError: 'float' object cannot be interpreted as an index

Oddly enough, this use case is excluded from all the documentation I came across. I would also agree to transform the matrix R into numpy array- since I want to iterate over the rows anyway.

0
source share
2 answers

Just use numpy.array():

from rpy2 import robjects
m = robjects.reval("matrix(1:6, nrow=2, ncol=3)")
import numpy as np
a = np.array(m)
+3
source

, convert_to_r_dataframe convert_robj. TO R, BACK R:

In [30]:
from rpy2 import robjects
m=robjects.r('matrix(1:6, nrow=2, ncol=3)')
In [31]:

print com.convert_robj(m)
   0  1  2
1  1  3  5
2  2  4  6
In [32]:

m=robjects.r('as.data.frame(matrix(1:6, nrow=2, ncol=3, dimnames=list(1:2, 1:3)))')
In [33]:

print com.convert_robj(m)
   1  2  3
1  1  3  5
2  2  4  6
+1

All Articles