[R] calculating an N50

Barry Rowlingson 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=rev(c(1,1,2,3,4,5,5,6,7,9,9,9))


  > x[cumsum(x) > sum(x)/2][1]
 [1] 7


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.


More information about the R-help mailing list