Extract all column values ​​in a differently defined row name in R

Suppose I have this matrix M:

V1 B001E4KFG0 A3SGXH7AUHU8GW B00813GRG4 A1D87F6ZCVE5NK B00813GRG4 ABXLMWJIXXAIN 

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

+5
source share
1 answer

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

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

using "B00813GRG4" because the row index returns only the first matching element, similar to using match .

  M[match('B00813GRG4', rownames(M)),, drop=FALSE] # V1 #B00813GRG4 "A1D87F6ZCVE5NK" 
+3
source

Source: https://habr.com/ru/post/1232902/


All Articles