Re: [R] how to reproduce the same sampled units?

From: Ted Harding <ted.harding_at_nessie.mcc.ac.uk>
Date: Thu, 03 May 2007 00:56:46 +0100 (BST)


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 ------------------------------

______________________________________________
R-help_at_stat.math.ethz.ch 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. Received on Thu 03 May 2007 - 00:03:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 May 2007 - 00:31:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.