[R] Selecting rows/columns of a matrix

Steven Yen syen04 at gmail.com
Sun Oct 26 19:35:40 CET 2014


Dear

I am interested in selecting rows and columns of a matrix with a 
criterion defined by a binary indicator vector. Let  matrix a be

 > a<-matrix(1:16, 4,4,byrow=T)
 > a
      [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    9   10   11   12
[4,]   13   14   15   16

Elsewhere in Gauss, I select the first and third rows and columns of 
a by defining a column vector j = [1,0,1,0]. Then, select the rows of 
a using j, and then selecting the rows of the transpose of the 
resulting matrix using j again. I get the 2 x 2 matrix as desired. Is 
there a way to do this in R? below are my Gauss commands. Thank you.

---

j

1
0
1
0

a=selif(a,j); a

1  2  3  4
9 10 11 12

a=selif(a',j); a

1  9
3 11



More information about the R-help mailing list