[R] calculating an N50

Greg Snow Greg.Snow at imail.org
Tue Nov 25 18:53:49 CET 2008


Try this:

> tmp <- sample( 100, 50 )
>
> tmp2 <- rev( sort(tmp) )
>
> tmp3 <- cumsum(tmp2) <= sum(tmp)/2
>
> sum(tmp3) # number needed
[1] 14
>
> tmp2[ sum(tmp3) ] # the smallest value
[1] 78
>
> sum(tmp2[tmp3]) / sum(tmp) # check
[1] 0.4894614
> sum(tmp2[ 1:(sum(tmp3)+1) ]) / sum(tmp)
[1] 0.519516

Hope this helps,


--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jeremy Leipzig
> Sent: Tuesday, November 25, 2008 10:45 AM
> To: r-help at r-project.org
> Subject: 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