-1

So I have a data.frame and I want to only get the lines that represent 80% of the total of one column.

This is an example of my data.frame :

This is an example of my data.frame

I've tried the quantiles but that doesn't give me the result I was expecting.

x <- 1
total <- 0
while (total <= sum(reference$CA) * 80 / 100) {
  total <- total + as.double(reference[x, "CA"])
  x = x + 1
}
reference_80 <- reference[(1:x - 1),]

I can imagine that R can provide such things but I wasn't able to find it yet, also if my data.frame is very large it'll take a lot of time with my code because I'm using a loop that's why I was wondering if there is a function that could do the job.

1 Answers1

1

Try this:

reference[cumsum(reference$CA) < sum(reference$CA) * .8,]
A. Stam
  • 2,148
  • 14
  • 29