Home > Net >  Search rownames with vector and report vector of row numbers
Search rownames with vector and report vector of row numbers

Time:01-26

I want to used a vector of values to search the rownames of a dataframe. I want the code to return a vector of row numbers associated with the rownames of interest. In the example below, if I provided c("GeneA","GeneC","GeneF") I would want c(1,3,6) returnd

The code I provide below reports a single rownumber from a single rowname. I haven't figured out a way to provide multiple searches and the search results I come up with only use a single search value.

x y z
GeneA 81.06 -1.59 0.29
GeneB 174.78 -1.57 0.28
GeneC 124.62 -1.49 0.22
GeneD 232.72 -1.45 0.25
GeneE 52.41 -1.41 0.33
GeneF 29.47 -1.40 0.39
which(rownames(df) == "GeneB")
[1] 2

CodePudding user response:

We may use match

match(c("GeneA","GeneC","GeneF"), rownames(df))
  •  Tags:  
  • r
  • Related