# Coin Toss Simulation

4 messages
Open this post in threaded view
|
Report Content as Inappropriate

## Coin Toss Simulation

 I am trying a simple toin coss simulation, of say 200 coin tosses. The idea is to have a data frame like so:     Experiment#    Number_Of_Heads      1                   104      2                    96      3                    101 So I do:  d <-data.frame(exp_num=c(1,2,3));  /* Just 3 experiments to begin with */  d\$head_ct <-sum(sample(0:1,200,repl=TRUE));  d;   exp_num    head_ct 1       1        85 2       2        85 3       3        85                             /* the same scalar value is applied to all the rows */ So I tried using "within", and "for", and making the "sum(   " as a function, and I end up with the same...I get the same constant value.  But what I want of course is different "samples"....
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin Toss Simulation

 try this: n <- 3 data.frame(Exp = seq_len(n),      Heads = rbinom(n, 200, 0.5)) I hope it helps. Best, Dimitris On 10/13/2010 7:28 PM, Shiv wrote: > > I am trying a simple toin coss simulation, of say 200 coin tosses. The idea > is to have a data frame like so: >      Experiment#    Number_Of_Heads >       1                   104 >       2                    96 >       3                    101 > > So I do: > >   d<-data.frame(exp_num=c(1,2,3));  /* Just 3 experiments to begin with */ >   d\$head_ct<-sum(sample(0:1,200,repl=TRUE)); >   d; >    exp_num    head_ct > 1       1        85 > 2       2        85 > 3       3        85                             /* the same scalar value is > applied to all the rows */ > > So I tried using "within", and "for", and making the "sum(   " as a > function, and I end up with the same...I get the same constant value.  But > what I want of course is different "samples".... > > > -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 Web: http://www.erasmusmc.nl/biostatistiek/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Coin Toss Simulation

 In reply to this post by Shiv Shiv wrote: > I am trying a simple toin coss simulation, of say 200 coin tosses. The idea > is to have a data frame like so: >     Experiment#    Number_Of_Heads >      1                   104 >      2                    96 >      3                    101 > > So I do: > >  d <-data.frame(exp_num=c(1,2,3));  /* Just 3 experiments to begin with */ >  d\$head_ct <-sum(sample(0:1,200,repl=TRUE)); /* */ is not a valid comment in R, and you don't need ';' either :) Just try evaluating the right hand side so that the R interpreter prints the result.  This is just calling sample once, and therefore you only get one number, 85 in your case. >  d; >   exp_num    head_ct > 1       1        85 > 2       2        85 > 3       3        85                             /* the same scalar value is > applied to all the rows */ > > So I tried using "within", and "for", and making the "sum(   " as a > function, and I end up with the same...I get the same constant value.  But > what I want of course is different "samples".... Look at ?replicate or just use ?rbinom directly d <-data.frame(exp_num = 1:3) d\$head_ct <- rbinom(nrow(d), 200, prob = 0.5) > > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.