Find row number from a row name in R
Asked Answered
H

1

10

How can I find the row number of a particular row name in R? I have found functions which allow you to find a row number for a particular matrix value, but not from a row name.

Hearten answered 15/7, 2015 at 17:25 Comment(1)
The answer is here: [#17733228Froze
G
16

You can use which

which(rownames(dat) == "theName") 
Geode answered 15/7, 2015 at 17:33 Comment(3)
Ok thanks. I did this, but this is what R returned: integer(0) It should be returning a row # somewhere in the 500s. Any ideas on what I may be doing wrong?Hearten
@JoelleMarie the name is probably wrong, eg which(c("a","b") == "c") returns integer(0)Geode
@Hearten in my case the labels of my dataset happened to have a lot of spaces after the name, so i had to include those spaces as part of the label I wanted to find to get it working. By typing row.names(dat) you should see if this was also your case, provided this is still of interest for you.Cuevas

© 2022 - 2024 — McMap. All rights reserved.