How do I stop set.seed() after a specific line of code?

Clyde Frog picture Clyde Frog · Mar 29, 2014 · Viewed 16.6k times · Source

I would like to end the scope of set.seed() after a specific line in order to have real randomization for the rest of the code. Here is an example in which I want set.seed() to work for "rnorm" (line 4), but not for "nrow" (line 9)

set.seed(2014)
f<-function(x){0.5*x+2}
datax<-1:100
datay<-f(datax)+rnorm(100,0,5)
daten<-data.frame(datax,datay)
model<-lm(datay~datax)
plot(datax,datay)
abline(model)
a<-daten[sample(nrow(daten),20),]
points(a,col="red",pch=16)
modela<-lm(a$datay~a$datax)
abline(modela, col="red")

Thanks for suggestions, indeed!

Answer

Carlton Chen picture Carlton Chen · Oct 25, 2018
set.seed(NULL)

See help documents - ?set.seed:

"If called with seed = NULL it re-initializes (see ‘Note’) as if no seed had yet been set."