I am trying to find the top cumulative amount of rows of a column up to a specified number. So if I have this data set
df <- data.frame(x = rnorm(26, 10, 2))
and I want the number of rows from the top that sums to 100. I have tried using variations of this:
df %>% top_n(6)
But that does not work. Is there a function that can do this easily that I am missing?
The use case for this is if I have a frequency column that gives me the percentage that an observation appears in a dataset after conducting group_by and desc functions and then I want the top, say 15%, of all observations.
Thanks in advance.
cumsum
. – Chirayu Chamolidf %>% arrange(desc(x)) %>% filter(cumsum(x) < 100)
. – tchakravarty