Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Thomas Lumley
A much easier way to do this, and what Kevin probably was looking for, is replicate(). replicate(100, sum(sample(1:6,10, replace=TRUE))) gives 100 sums of 10 fair dice. -thomas On Mon, 8 Oct 2007, Alberto Monteiro wrote: > Kevin Zembower wrote: >> >> I'm trying to get R to simulate

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Julian M Burgos
Well, if the dice are fair, then all rolls are independent. If you want to roll each dice 100 times, you can do something like rolls=matrix(sample(1:6,1000,replace=T),ncol=10) apply(rolls,2,sum) I'm sure that there must be a more elegant way to do it, though. Julian On Mon, 8 Oct 2007, Zemb

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Alberto Monteiro
Kevin Zembower wrote: > > I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: > > rep(sum(sample(1:6,100,replace=T)), times=10) > [1] 341 341 341 341 341 341 341 341 341 341 > rep(s

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Richard M. Heiberger
rep( sum(sample(1:6,100,replace=T)), times=10) Read carefully what you wrote. You asked R to sum a single sample, then make ten copies of the sum. You need to take ten samples, then sum each. apply() would be helpful. -Original Message- I'm trying to get R to simulate the sum

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Zembower, Kevin
ject: Re: [R] Dice simulation: Getting rep to re-evaluate sample()? See ?replicate which I think is what you are after. Chuck On Mon, 8 Oct 2007, Zembower, Kevin wrote: > I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Prof Brian Ripley
See ?replicate, e.g. replicate(sum(sample(1:6,100,replace=TRUE)), n=10) Function arguments are (in gneral) evaluated as if they were evaluated when passed to the function. On Mon, 8 Oct 2007, Zembower, Kevin wrote: > I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes,

Re: [R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Charles C. Berry
See ?replicate which I think is what you are after. Chuck On Mon, 8 Oct 2007, Zembower, Kevin wrote: > I'm trying to get R to simulate the sum of the values on 10 fair dice > (yes, it's related to a homework problem, but is not the problem > itself). I tried to do this: >> rep(sum(s

[R] Dice simulation: Getting rep to re-evaluate sample()?

2007-10-08 Thread Zembower, Kevin
I'm trying to get R to simulate the sum of the values on 10 fair dice (yes, it's related to a homework problem, but is not the problem itself). I tried to do this: > rep(sum(sample(1:6,100,replace=T)), times=10) [1] 341 341 341 341 341 341 341 341 341 341 and noticed that sum(sample()) seems to