1

I am trying to write a one-liner in R that finds the top records in each class in a dataframe. I have found this excellent example of using mtcars() to such a case.

I will stick to the same example whereby my class is "cyl" and I am trying to get to the top values of the column "hp".

The answer given to the previous question gives the "Top N values" via head/tail function.

require(data.table)
d <- data.table(mtcars, key="cyl")
d[, head(.SD, 3), by=cyl]

I am trying to do the exact same thing using percentages. e.g. the top 80% hp rows (ranked by order from largest to smallest hp) for each class.

Is there a way to incorporate the percentage perspective into the data.table function above?

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Learner123
  • 87
  • 5

1 Answers1

3
d <- d[order(cyl,-hp)]
d[,.SD[hp >= quantile(hp, 0.8)], by = cyl]


    cyl  mpg  disp  hp drat    wt  qsec vs am gear carb
 1:   4 30.4  95.1 113 3.77 1.513 16.90  1  1    5    2
 2:   4 21.4 121.0 109 4.11 2.780 18.60  1  1    4    2
 3:   4 21.5 120.1  97 3.70 2.465 20.01  1  0    3    1
 4:   6 19.7 145.0 175 3.62 2.770 15.50  0  1    5    6
 5:   6 19.2 167.6 123 3.92 3.440 18.30  1  0    4    4
 6:   6 17.8 167.6 123 3.92 3.440 18.90  1  0    4    4
 7:   8 15.0 301.0 335 3.54 3.570 14.60  0  1    5    8
 8:   8 15.8 351.0 264 4.22 3.170 14.50  0  1    5    4
 9:   8 14.3 360.0 245 3.21 3.570 15.84  0  0    3    4
10:   8 13.3 350.0 245 3.73 3.840 15.41  0  0    3    4

Or with dplyr:

library(dplyr)
d %>% 
  group_by(cyl) %>% 
  filter(hp >= quantile(hp, 0.8))
Lennyy
  • 5,932
  • 2
  • 10
  • 23