Dear All, This is hard to describe so I made a simple example. set.seed(1001) total <- 0 data <- vector("list", 30) for(i in 1:30) { data[[i]] <- runif(50) } Let's call a data set runif(50). While the for loop is running, 100 data sets are generated. I want to restore 23th data set (the data set generated in 23th for loop) without the loop. I've tried set.seed(1023) runif(50) but this is different data from the data set gotten from 23th for loop. How can I get 23th data set without the loop? Thank you, Soyeon
On 09/06/2011 11:14 AM, Soyeon Kim wrote:> Dear All, > > This is hard to describe so I made a simple example. > set.seed(1001) > total<- 0 > data<- vector("list", 30) > for(i in 1:30) { > data[[i]]<- runif(50) > } > Let's call a data set runif(50). > While the for loop is running, 100 data sets are generated. > I want to restore 23th data set (the data set generated in 23th for > loop) without the loop. > I've tried set.seed(1023) runif(50) > but this is different data from the data set gotten from 23th for loop. > How can I get 23th data set without the loop?You can't. To get the 23rd value again, set the seed to 1001, run the loop 22 times, then the next one will be the 23rd. Duncan Murdoch
What about: set.seed(1001) total <- 0 data <- vector("list", 30) for(i in 1:30) { data[[i]] <- runif(50) } set.seed(1001) data[[23]] <- runif(50) HTH Samuel -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Soyeon Kim Sent: 09 June 2011 16:15 To: r-help Subject: [R] set.seed and for loop Dear All, This is hard to describe so I made a simple example. set.seed(1001) total <- 0 data <- vector("list", 30) for(i in 1:30) { data[[i]] <- runif(50) } Let's call a data set runif(50). While the for loop is running, 100 data sets are generated. I want to restore 23th data set (the data set generated in 23th for loop) without the loop. I've tried set.seed(1023) runif(50) but this is different data from the data set gotten from 23th for loop. How can I get 23th data set without the loop? Thank you, Soyeon ______________________________________________ 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. __________ Information from ESET NOD32 Antivirus, version of virus signature database 6193 (20110609) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com __________ Information from ESET NOD32 Antivirus, version of virus signature database 6193 (20110609) __________ The message was checked by ESET NOD32 Antivirus. http://www.eset.com
There are certainly people that would know how the random functions work better than I, but I believe you would need to simulate 22 datasets and then get the 23rd dataset. So to restore the 23rd: set.seed(1001) for(i in 1:22){ garbage = runif(50) } data[[23]] = runif(50) Hope that helps, Sam On Thu, Jun 9, 2011 at 12:14 PM, Soyeon Kim <yunni0731 at gmail.com> wrote:> Dear All, > > This is hard to describe so I made a simple example. > set.seed(1001) > total <- 0 > data <- vector("list", 30) > for(i in 1:30) { > ?data[[i]] <- runif(50) > } > Let's call a data set runif(50). > While the for loop is running, 100 data sets ?are generated. > I want to restore 23th data set (the data set generated in 23th for > loop) without the loop. > I've tried set.seed(1023) runif(50) > but this is different data from the data set gotten from 23th for loop. > How can I get 23th data set without the loop? > > Thank you, > Soyeon > > ______________________________________________ > 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. >
On Thu, Jun 09, 2011 at 10:14:48AM -0500, Soyeon Kim wrote:> Dear All, > > This is hard to describe so I made a simple example. > set.seed(1001) > total <- 0 > data <- vector("list", 30) > for(i in 1:30) { > data[[i]] <- runif(50) > } > Let's call a data set runif(50). > While the for loop is running, 100 data sets are generated. > I want to restore 23th data set (the data set generated in 23th for > loop) without the loop. > I've tried set.seed(1023) runif(50) > but this is different data from the data set gotten from 23th for loop. > How can I get 23th data set without the loop?It is possible to save a bit (not much) over the loop, since a sequence of calls of runif() creates the numbers from the same sequence of numbers. So it is possible to get more numbers in one call not changing the rest of the sequence. I mean the following set.seed(1001) total <- 0 data <- vector("list", 30) for(i in 1:30) { data[[i]] <- runif(50) } set.seed(1001) garbage <- runif(22*50) recomp <- runif(50) identical(data[[23]], recomp) [1] TRUE There are algorithms for jumping ahead in the sequence without generating all intermediate numbers, but i do not know about an efficient available implementation. Petr Savicky.