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.
Asked
Active
Viewed 7,714 times
9
-
1The answer is here: [https://stackoverflow.com/questions/17732728/referencing-row-number-in-r][1] – David Medrano Mar 10 '20 at 15:30
1 Answers
15
You can use which
which(rownames(dat) == "theName")

Rorschach
- 31,301
- 5
- 78
- 129
-
1Ok 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? – user507 Jul 15 '15 at 19:24
-
@JoelleMarie the name is probably wrong, eg `which(c("a","b") == "c")` returns `integer(0)` – Rorschach Jul 15 '15 at 19:28
-
@user507 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. – ForEverNewbie Jun 01 '20 at 05:54