[R] Selecting rows/columns of a matrix

Rui Barradas ruipbarradas at sapo.pt
Sun Oct 26 19:49:18 CET 2014


Sorry, that should be

t(a[as.logical(j), as.logical(j)])

Rui Barradas

Em 26-10-2014 18:45, Rui Barradas escreveu:
> Hello,
>
> Try the following.
>
> a[as.logical(j), as.logical(j)]
>
> # or
> b <- a[as.logical(j), ]
> t(b)[as.logical(j), ]
>
>
> Hope this helps,
>
> Rui Barradas
>
> Em 26-10-2014 18:35, Steven Yen escreveu:
>> 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
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list