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?