How can I import a matrix from R saved as RData to a pandas data frame without losing the column names of the R matrix?
For example, if I have saved this matrix in R:
A = matrix(
c(2, 4, 3, 1, 5, 7), # the data elements
nrow=2, # number of rows
ncol=3, # number of columns
byrow = TRUE) # fill matrix by rows
dimnames(A) = list(
c("row1", "row2"), # row names
c("col1", "col2", "col3")) # column names
A
save (A, file = 'matrix.RData')
outputs:
> A
col1 col2 col3
row1 2 4 3
row2 1 5 7
Then loaded in python with rpy2 as follows:
from __future__ import print_function
from rpy2.robjects import pandas2ri,r
import rpy2.robjects as robjects
def main():
pandas2ri.activate()
r['load']('matrix.RData')
variables = tuple(robjects.globalenv.keys())
print('variables: {0}'.format(variables))
matrix = robjects.globalenv['A']
frame = pandas2ri.ri2py(matrix)
print(frame)
print('type(frame): {0}'.format(type(frame)))
if __name__ == "__main__":
main()
which prints:
variables: ('A',)
[[ 2. 4. 3.]
[ 1. 5. 7.]]
type(frame): <type 'numpy.ndarray'>
The matrix has lost his column names. I would like to keep them by loading the R into a pandas data frame.