How can I use the row.names attribute to order the rows of my dataframe in R?

tumultous_rooster picture tumultous_rooster · Nov 30, 2013 · Viewed 60.5k times · Source

I created a random forest and predicted the classes of my test set, which are living happily in a dataframe:

row.names   class  
564028      1
275747      1
601137      0
922930      1
481988      1
...

The row.names attribute tells me which row is which, before I did various operations that scrambled the order of the rows during the process. So far so good.

Now I would like get a general feel for the accuracy of my predictions. To do this, I need to take this dataframe and reorder it in ascending order according to the row.names attribute. This way, I can compare the observations, row-wise, to the labels, which I already know.

Forgive me for asking such a basic question, but for the life of me, I can't find a good source of information regarding how to do such a trivial task.

The documentation implores me to:

use attr(x, "row.names") if you need to retrieve an integer-valued set of row names.

but this leaves me with nothing but NULL.

My question is, how can I use row.names which has been loyally following me around in the various incarnations of dataframes throughout my workflow? Isn't this what it is there for?

Answer

cburghard picture cburghard · Jan 29, 2015

This worked for me:

new_df <- df[ order(row.names(df)), ]