'Why is rpy2 not converting a numpy array into an R matrix?

I inherited some code that uses rpy2. However, the command that is supposed to convert the numpy arrays into R matrices is't working and I can't figure out the reason why or a workaround.

import numpy as np
import rpy2.robjects as ro

dist_square = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
assert dist_square.ndim == 2
assert dist_square.shape[0] == dist_square.shape[1]

# convert distance matrix to R
m = ro.vectors.Matrix(dist_square)

Running the above piece of code produces the following error:

m = ro.vectors.Matrix(dist_square)
TypeError: Matrix() takes no arguments

Why is this error and is there a way to correct/circumvent it?



Solution 1:[1]

Try using the constructor in the R base package. See:

https://rpy2.github.io/doc/v3.5.x/html/vector.html#matrix

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lgautier