[R] how to reproduce the same sampled units?

(Ted Harding) ted.harding at nessie.mcc.ac.uk
Thu May 3 01:56:46 CEST 2007


On 02-May-07 23:25:18, Santanu Pramanik wrote:
> Hi all,
>  
> Is it possible to generate the same sample number of times in R?
> In SAS, using the option "seed" it is possible to reproduce exactly
> the same sample. Is there any such feature in R which I can use? 
>  
> For further clarity,
>  
> for (i in 1:2)
> {
> samp = sample(1:1000,100,replace = FALSE)
> print(samp)
> }

> > For the above simulation, is it possible to generate the same sampled
> units twice?
>  
> Any suggestion would be gratefully acknowledged.
> Thanks,
> Santanu

Have a look at set.seed():

  ?set.seed

You will of course have to invoke it before you do your first run.
You cannot find out what the seed was afterwards!

For example (your example somewhat reduced):

  for (i in 1:2)
  {
  samp = sample(1:1000,10,replace = FALSE)
  print(samp)
  }
# [1] 984 587  26 920 304 247 434 392 650 279
# [1] 178 619 458 208 389 629 988 263 598 308

  for (i in 1:2)
  {
  set.seed(512534)
  samp = sample(1:1000,10,replace = FALSE)
  print(samp)
  }
# [1] 271 106 570 621 257 663 399 454 983 805
# [1] 271 106 570 621 257 663 399 454 983 805

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 03-May-07                                       Time: 00:56:43
------------------------------ XFMail ------------------------------



More information about the R-help mailing list