2008/11/25 Jeremy Leipzig <[EMAIL PROTECTED]>: >> Given a set of integers of different values how do I calculate the >> minimum number of the largest of integers that are required, when >> summed, to equal 50% of the total sum of the the set? >> > Actually I need the value of the smallest member such that the > sum of all members equal or greater to that is 50% of the total sum of the set
How's this? For x sorted decreasing: > x=rev(c(1,1,2,3,4,5,5,6,7,9,9,9)) do: > x[cumsum(x) > sum(x)/2][1] [1] 7 check: What value are we after? > sum(x)/2 [1] 30.5 Sum of all x >=8 is too small: > sum(x[x>=8]) [1] 27 Sum of all x >= 7 is big enough: > sum(x[x>=7]) [1] 34 Is that right? Basically it uses cumsum to get the cumulative sum and then finds the first one that goes over the half-way mark. Barry ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.