Harjeet Singh Bariar
Harjeet Singh Bariar

Reputation: 130

r row names selection using columns

Suppose i have this matrix

 
      0 1 2 3 4 5 6 98 183 385 419 420 422 423 469 470 35698 35709 35729 37415
0     0 1 1 1 0 0 1  0   0   1   0   0   0   0   1   0     0     0     0     1
1     1 0 1 0 0 1 1  0   0   1   0   0   0   0   1   0     0     0     0     0
2     1 1 0 1 1 0 0  0   0   0   1   0   1   0   1   0     0     0     0     0
3     1 0 1 0 1 1 0  1   1   0   1   1   1   1   0   0     1     0     0     1
4     0 0 1 1 0 1 1  1   0   0   1   1   1   0   0   1     0     1     1     0
5     0 1 0 1 1 0 1  1   0   0   0   1   0   0   0   1     0     0     1     0
6     1 1 0 0 1 1 0  1   0   1   0   0   0   0   0   0     0     0     0     0
98    0 0 0 1 1 1 1  0   0   0   0   1   0   0   0   1     0     0     1     0
183   0 0 0 1 0 0 0  0   0   0   0   0   1   1   0   0     0     0     0     1
385   1 1 0 0 0 0 1  0   0   0   0   0   0   0   0   0     0     0     0     0
419   0 0 1 1 1 0 0  0   0   0   0   1   1   0   0   0     1     1     0     0
420   0 0 0 1 1 1 0  1   0   0   1   0   0   0   0   0     1     1     0     0
422   0 0 1 1 1 0 0  0   1   0   1   0   0   1   1   0     0     0     0     1
423   0 0 0 1 0 0 0  0   1   0   0   0   1   0   0   0     0     0     0     1
469   1 1 1 0 0 0 0  0   0   0   0   0   1   0   0   0     0     0     0     1
470   0 0 0 0 1 1 0  1   0   0   0   0   0   0   0   0     0     0     1     0
35698 0 0 0 1 0 0 0  0   0   0   1   1   0   0   0   0     0     0     0     0
35709 0 0 0 0 1 0 0  0   0   0   1   1   0   0   0   0     0     0     0     0
35729 0 0 0 0 1 1 0  1   0   0   0   0   0   0   0   1     0     0     0     0
37415 1 0 0 1 0 0 0  0   1   0   0   0   1   1   1   0     0     0     0     0  

I am getting a value from another program let us say x=3.

I want to choose the name of rows where x == 1 i.e. where the value of 3 is 1.

Output will be : 0,2,4,5,98,183,419,420,422,423,35698,37415. And I don't want to pass "3" directly into the command. I want to pass the variable x so that if this number varies I could get the output accordingly.

Can anyone help me, please? thanks in advance

Upvotes: 0

Views: 68

Answers (1)

dondapati
dondapati

Reputation: 849

x=matrix(c(1,1,2,5,6,6,5,7,7,8,3,3,1,9,20,20,4,7,9,5),4,5,dimnames = list(c(letters[1:4]),c(LETTERS[1:5])))

you'r requirement is row names then

rownames(x)[x[,"D"]==20]

here '20' is you'r input value and D is you'r searching column.

Upvotes: 1

Related Questions