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
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.