How to get top n companies from a data frame in decreasing order
问题 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) Now I would like to get the top 50 observations from this sorted vector. 回答1: head and tail are really useful functions! head(sort(Forbes2000$profits,decreasing=TRUE), n = 50) 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