Make a table showing the 10 largest values of a variable in R?

nikUoM picture nikUoM · Aug 11, 2015 · Viewed 35.1k times · Source

I want to make a simple table that showcases the largest 10 values for a given variable in my dataset, as well as 4 other variables for each observation, so basically a small subset of my data. It would look something like this:

Score  District  Age  Group  Gender
17     B         23    Red   1
12     A         61    Red   0
11.7   A         18    Blue  0
10     B         18    Red   0
.
.
etc.

whereby the data is ordered on the Score var. All the data is contained within the same dataframe.

Answer

Gaurav picture Gaurav · Aug 11, 2015

This should do it...

data <- data[with(data,order(-Score)),]

data <- data[1:10,]