[R] calculating an N50
b.rowlingson at lancaster.ac.uk
Tue Nov 25 18:54:33 CET 2008
2008/11/25 Jeremy Leipzig <jeremy at zigster.com>:
>> 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[cumsum(x) > sum(x)/2]
What value are we after?
Sum of all x >=8 is too small:
Sum of all x >= 7 is big enough:
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.
More information about the R-help