rlcorp
2012-Nov-09 17:57 UTC
[R] Counting the numbers of items in vector according to their size
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, but am guessing there is a density r function that may be easier then that. -- View this message in context: http://r.789695.n4.nabble.com/Counting-the-numbers-of-items-in-vector-according-to-their-size-tp4649085.html Sent from the R help mailing list archive at Nabble.com.
Rui Barradas
2012-Nov-09 21:22 UTC
[R] Counting the numbers of items in vector according to their size
Hello, As you know R better it will take you less and less time to get it right, and almost surely less and less lines of code to do the same thing. Here's a one liner: set.seed(1510) s=numeric(length=100000) for(i in 1:100000){ pop=(rbeta(n=20,shape1=2,shape2=1)) s[i]=sum(pop) } set.seed(1510) s2 <- replicate(1e5, {sum(rbeta(n=20,shape1=2,shape2=1))}) identical(s, s2) # TRUE As for your question, hist(s, breaks="Scott") # Looks normal to me You can use asymptotic normal theory. Hope this helps, Rui Barradas Em 09-11-2012 17:57, rlcorp escreveu:> 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, but am guessing there is > a density r function that may be easier then that. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Counting-the-numbers-of-items-in-vector-according-to-their-size-tp4649085.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.