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"
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With