[R] calculating an N50

Daniel Malter daniel at umd.edu
Tue Nov 25 19:28:04 CET 2008


Greg's solution is most elegant (I think). This is more of an illustrative
approach:

set.seed(1) #to replicate identical sampling if you use this code

x=sample(1:100,replace=T)

x=rev(sort(x)) #reverse order

sum(x)/2  # what is the mean of x: 2613.5

cumsum(x) # the cumulative sums for x=1:i

all.greater=which(cumsum(x)>sum(x)/2) # which cumulative sums are greater
than your critical value

first=min(all.greater) #what is the first cumulative sum that is greater
than your critical value: 32

x[first] # what is x at "first": 72

#or integrating the above:

x[min(which(cumsum(x)>sum(x)/2))]



Cheers,
Daniel

-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Jeremy Leipzig
Gesendet: Tuesday, November 25, 2008 12:45 PM
An: r-help at r-project.org
Betreff: Re: [R] calculating an N50

> 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

______________________________________________
R-help at 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.



More information about the R-help mailing list