如何使用row.names属性来排序R中的数据帧行? [英] How can I use the row.names attribute to order the rows of my dataframe in R?
问题描述
我创建了一个随机的森林,并预测了我的测试集的类,它们在数据帧中幸福地生活:
row.names class
564028 1
275747 1
601137 0
922930 1
481988 1
...
row.names
属性告诉我哪些行是在进程之前进行各种操作来扰乱行的顺序。到目前为止这么好。
现在我想得到我的预测的准确性的一般感觉。为此,我需要使用这个数据框并按照 row.names
属性的升序重新排序。这样,我可以将观察结果逐行对比,我已经知道的标签。
请原谅我提出这样一个基本问题,但对于生活对于我来说,我找不到关于如何做这样一个微不足道的任务的很好的信息来源。
该文档介绍给我:
使用
attr(x,row.names)
如果需要检索整数值集行名称。
但是这让我没有任何东西,但 NULL
p>
我的问题是,如何在数据帧的各种形式中一直忠实地跟踪我的 row.names
在我的工作流程?这不是吗?
这对我有用:
new_df< - df [order(row.names(df))]]
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?
This worked for me:
new_df <- df[ order(row.names(df)), ]
这篇关于如何使用row.names属性来排序R中的数据帧行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!