Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 727
nikUoM Avatar asked Aug 11 '15 10:08

nikUoM


2 Answers

From dplyr >= 1.0.0, we can use slice_max function.

library(dplyr)

mtcars %>% slice_max(mpg, n = 4)

#                mpg cyl disp  hp drat    wt  qsec vs am gear carb
#Toyota Corolla 33.9   4 71.1  65 4.22 1.835 19.90  1  1    4    1
#Fiat 128       32.4   4 78.7  66 4.08 2.200 19.47  1  1    4    1
#Honda Civic    30.4   4 75.7  52 4.93 1.615 18.52  1  1    4    2
#Lotus Europa   30.4   4 95.1 113 3.77 1.513 16.90  1  1    5    2

By default rows with ties are selected, if you want to ignore ties and strictly return n rows use with_ties = FALSE.

like image 91
Ronak Shah Avatar answered Oct 22 '22 05:10

Ronak Shah


This should do it...

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

data <- data[1:10,]

like image 42
Gaurav Avatar answered Oct 22 '22 05:10

Gaurav