Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract all column values row-wise by a particular row name in R

Tags:

r

matrix

Suppose I have this matrix M:

           V1
B001E4KFG0 A3SGXH7AUHU8GW
B00813GRG4 A1D87F6ZCVE5NK
B00813GRG4 ABXLMWJIXXAIN

Now if I want to extract all column values by the rowname "B00813GRG4" what should I do. I tried M["B00813GRG4",] but it gives me only "A1D87F6ZCVE5NK" and not both "A1D87F6ZCVE5NK" and "ABXLMWJIXXAIN"

like image 657
dothermitian Avatar asked Oct 19 '22 01:10

dothermitian


1 Answers

We can use == to return a logical vector and then it can be used to subset the rows.

M[rownames(M)=='B00813GRG4',, drop=FALSE]
#            V1              
# B00813GRG4 "A1D87F6ZCVE5NK"
# B00813GRG4 "ABXLMWJIXXAIN" 

using the 'B00813GRG4' as row index will return only the first matching element similar to using match.

 M[match('B00813GRG4', rownames(M)),, drop=FALSE]
 #           V1              
 #B00813GRG4 "A1D87F6ZCVE5NK"
like image 120
akrun Avatar answered Oct 21 '22 17:10

akrun