Find row name in R

微笑、不失礼 提交于 2019-12-20 07:51:13

问题


I wonder how I can extract 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?

thanks


回答1:


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"


来源:https://stackoverflow.com/questions/24508119/find-row-name-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!