[R] Simple simulations
jim holtman
jholtman at gmail.com
Tue Jun 28 01:58:26 CEST 2011
Here is another solution if you want to count a sequence like 00000 as
three overlapping runs of 3 zeros.
> x <- sample(c(rep(0,20), rep(1,37)), 1000000, TRUE) # vector of 1M
> z <- rle(x) # get the runs
> indx <- which(z$values == 0 & z$lengths > 2) # find the runs
> # now determine the number of 3 zeros
> # if you have 00000 then this is 3 runs of three
> sum(z$lengths[indx] - 2)
[1] 43353
>
On Mon, Jun 27, 2011 at 5:53 PM, Alexander Engelhardt
<alex at chaotic-neutral.de> wrote:
> Here's a one-liner. Let's see their software do that!
>
> sum(replicate(100, sum(sample(c(rep(0, 20), seq(1:37)), 3, replace = FALSE))
> == 0))
>
>
>
> Am 27.06.2011 23:08, schrieb robcinm:
>>
>> I am taking a basic statistics course this summer, and while the majority
>> of
>> the class is using a statistical package that came with the book, I am
>> doing
>> everything in R for practical reasons. Forgive me if there is
>> documentation/instruction easily available on this topic, but Google and
>> forums searches left me with nothing.
>>
>> We are learning about randomness at the moment and are required to create
>> simulations to use as an example. For the problem in the text, there are
>> 57
>> numbers with 1 through 20 belonging to the same “group”, meaning the
>> probability that a number in that group is picked is 20/57, and 21 through
>> 57 are individuals, belonging to no larger group. The point of the
>> exercise
>> is to simulate the probability of 1 through 20 being chosen at random
>> (with
>> no replacement) three times in a row through hundreds of trials. This is
>> the
>> basic syntax I have been using…
>>
>> sample(c(rep(0, 20), seq(1:37)), 3, replace = F)
>>
>> That works just fine, but I am wondering if there is a more efficient way
>> of
>> doing this. Right now, I am hitting the up arrow and hitting “enter”
>> hundreds of times and making note of each time a trial results in 0,0,0.
>> If
>> I place this syntax in a rep function, it just repeats the same output of
>> a
>> single sample x times instead of giving me new data.
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Simple-simulations-tp3628863p3628863.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.
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
More information about the R-help
mailing list