[R] calculating an N50

Jeremy Leipzig jeremy at zigster.com
Tue Nov 25 18:37:40 CET 2008


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?

For example,
> length(myTable$lgth)
[1] 303403
> sum(myTable$lgth)
[1] 4735396

I know through brute force that the N50 of the following set is 42 but
I don't know how to solve this without iteration
> sum(myTable$lgth[which(myTable$lgth>=42)])
[1] 2374260



More information about the R-help mailing list