2

I wonder how I can extract the row name from row number?

x <- data.frame( A = 1:10, B = 21:30 )
rownames( x ) <- sample( LETTERS, 10 )
> x
   A  B
J  1 21
A  2 22
I  3 23
G  4 24
H  5 25
B  6 26
P  7 27
Z  8 28
O  9 29
R  10 30
> x[ "H",]
  A  B 
H 5 25

I want to find what is the row name of specific row? for example row name of row=3

also which rowname contains the value 30?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Cina
  • 9,759
  • 4
  • 20
  • 36
  • or `rownames(x[3, ])` – Andrie Jul 01 '14 at 10:38
  • @DavidArenburg rownames(x)[3] works properly in my real data frame (includes 50 columns with rownames), but rownames(x[3, ]) returns NULL !!?? do you why? – Cina Jul 01 '14 at 10:59
  • I can't reproduce your error so I can't tell you why. If `rownames(x)[3]` works for you, then stick with it and don't brake your head on philosophical questions too much – David Arenburg Jul 01 '14 at 11:04

1 Answers1

8
set.seed(42)
x <- data.frame( A = 1:10, B = 21:30 )
rownames( x ) <- sample( LETTERS, 10 )
x
##    A  B
## X  1 21
## Z  2 22
## G  3 23
## T  4 24
## O  5 25
## K  6 26
## V  7 27
## C  8 28
## L  9 29
## R 10 30
rownames(x)[3] #third row name
## [1] "G"
rownames(x)[x$B == 30]
## [1] "R"
bartektartanus
  • 15,284
  • 6
  • 74
  • 102