[R] Finding the percentage of points in a designated vector

David Winsemius dwinsemius at comcast.net
Fri Nov 9 21:10:54 CET 2012


On Nov 9, 2012, at 9:52 AM, Ricky Corp wrote:

> I am new to R and learned to program 10 years ago in C++. I am currently
> working a project that looks at the distribution of randomly generated beta
> values. I take 20 random beta values find their sum, repeat 100000 times.
> 
> Here is my code that it took me 4 hours to get
> 
> s=numeric(length=100000)
> for(i in 1:100000){
>  pop=(rbeta(n=20,shape1=2,shape2=1))
>  s[i]=sum(pop)
> }
> 
> So now I have them all in in vector, I would like to maybe sort or count
> them to see how many are less than or equal to 10,

sum( s <= 10 ) # Adding up TRUE/FALSE as 1/0

> but am guessing there is
> a density r function that may be easier then that.

Your subject line suggested something else however. There is a quantile function. Perhaps:

quantile(s, c(0.025, 0.975) )  # 2.5th and 97.5th percentile points

-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list