9

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.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
user507
  • 223
  • 1
  • 6
  • 14

1 Answers1

15

You can use which

which(rownames(dat) == "theName") 
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • 1
    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? – 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