edesz
edesz

Reputation: 12406

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?

Upvotes: 0

Views: 356

Answers (1)

lgautier
lgautier

Reputation: 11555

Did you try this ?

b = m.rx(True, IntVector((2,3)))

Upvotes: 1

Related Questions