How to get top n companies from a data frame in decreasing order

后端 未结 4 912
一向
一向 2020-12-03 08:50

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,decreas         


        
4条回答
  •  误落风尘
    2020-12-03 09:22

    Mnel is right that in general, You want to use head() and tail() functions along with the a sorting function. I should mention though for medium data sets Vince's method works faster. If you didn't use head() or tail(), then you could used the basic subsection call operator []....

     library(plyr)
     x = arrange(Forbes2000,desc(profits))
     x = x[1:50,]
     #Or using Order
     x = Forbes2000[order(Forbes2000$profits, decreasing= T),]
     x = x[1:50,]
    

    However, I really do recommend the head(), tail(), or filter() functions because the regular [] operator assumes your data is structured in easily drawn array or matrix format. (Hopefully, this answers Teja question)

    Now which pacakage you choose is largely subjective. However reading people's comments, I will say that the choice to use plyr's arrange(), {bases}'s order() with {utils} head() and tails, or plyr() largely depends on the memory size and row size of your dataset. I could go into more detail about how Plyr and sometimes Dplyr have problems with large complex datasets, but I don't want to get off topic.

    P.S. This is one of my first times answering so feedback is appreciated.

提交回复
热议问题