如何以降序从数据框中获取前n家公司 [英] How to get top n companies from a data frame in decreasing order
本文介绍了如何以降序从数据框中获取前n家公司的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从数据框中获得排名前n位的公司.以下是我的代码.
I am trying to get the top 'n' companies from a data frame.Here is my code below.
data("Forbes2000", package = "HSAUR")
sort(Forbes2000$profits,decreasing=TRUE)
现在我想从排序后的向量中获得前50个观察值.
Now I would like to get the top 50 observations from this sorted vector.
推荐答案
head
和tail
确实有用!
head(sort(Forbes2000$profits,decreasing=TRUE), n = 50)
如果要data.frame的前50行,则可以使用plyr
中的arrange
函数对data.frame
进行排序,然后使用head
If you want the first 50 rows of the data.frame, then you can use the arrange
function from plyr
to sort the data.frame
and then use head
library(plyr)
head(arrange(Forbes2000,desc(profits)), n = 50)
请注意,我在调用desc
的过程中包装了profits
,这意味着它将以降序排列.
Notice that I wrapped profits
in a call to desc
which means it will sort in decreasing order.
在没有工作的情况下工作
To work without plyr
head(Forbes2000[order(Forbes2000$profits, decreasing= T),], n = 50)
这篇关于如何以降序从数据框中获取前n家公司的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文