Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting row labels (?) from a data.frame

Tags:

dataframe

r

Starting with a data.frame...

df = data.frame(k=c(1,5,4,7,6), v=c(3,1,4,1,5))
> df
  k v
1 1 3
2 5 1
3 4 4
4 7 1
5 6 5

I might run some number of arbitrary manipulations...

> foo1 = df[df$k>3,]
> foo2 = head(foo1[order(foo1$v),], 2)
> foo2
  k v
2 5 1
4 7 1

At this point foo2 has somehow retained the original row numbers fromdf (in this case 2 and 4).

How do I extract these?

> insert_magic_function_here(foo2)
[1] 2 4
like image 875
mat kelcey Avatar asked Sep 03 '26 10:09

mat kelcey


1 Answers

I think you're looking for rownames.

like image 188
joran Avatar answered Sep 06 '26 00:09

joran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!