W R W R -4 years ago 123
R Question

rPy2 slice matrix

I have this matrix in

rPy2
in Python 2.7.

m = robjects.r.matrix(robjects.IntVector(range(15)), nrow=5)
print(m)
[,1] [,2] [,3]
[1,] 0 5 10
[2,] 1 6 11
[3,] 2 7 12
[4,] 3 8 13
[5,] 4 9 14


I am trying extract 2 slices from this matrix. In the first slice (1. below) I need to extract all columns for a range of rows. In the second slice (2. below), I need to extract all rows for a range of columns.

1.

I need to extract all columns for rows 2-4:

[2,] 1 6 11
[3,] 2 7 12
[4,] 3 8 13


I tried this:

a = m.rx([2:5], True)


but it does not accept a Python list.

2.

I need to extract all the rows from columns 2,3:

[,2] [,3]
[1,] 5 10
[2,] 6 11
[3,] 7 12
[4,] 8 13
[5,] 9 14


Again, I do not know how to specify a list of columns:

b = m.rx(True, [2:4])


does not work.

How can I achieve these slices
a
and
b
of the matrix
m
in
rPy2
?

Answer Source

Did you try this ?

b = m.rx(True, IntVector((2,3)))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download