Reputation: 13103
I am running an R
script that reads an external RData
file. I am attempting to modify that R
script to run without using that RData
file to improve generality.
I discovered that the results returned by the script differ depending on whether I use a set.seed
statement immediately before or immediately after loading the RData
file. I also discovered that the RData
file contains a vector called .Random.seed
. Otherwise the RData
file only contains data as far as I can tell, not any functions or code.
The mere presence of the .Random.seed
vector in the RData
file appears to change the results of random number generators (runif
, rnorm
, etc.) or sample
statements in the script that appear after I load the RData
file even though I never refer to this .Random.seed
vector in the script.
How can I reproduce the results of the script that contains the RData
file without actually including the RData
file in the script?
load("CPT CAL.RData")
.Random.seed[1:5]
#[1] 403 493 -1955963110 -1150619109 212268921
sample(1:100, 10, replace = TRUE)
#[1] 100 60 76 39 7 57 28 85 89 73
sample(1:100, 10, replace = TRUE)
#[1] 81 89 15 4 60 32 58 71 62 17
sample(1:100, 10, replace = TRUE)
#[1] 18 46 89 66 23 24 24 41 37 70
##################################
# Here I try to reproduce the above results using the first element
# of the .Random.seed vector as a seed but this does not work
set.seed(403)
sample(1:100, 10, replace = TRUE)
#[1] 44 31 23 83 16 27 71 64 13 18
sample(1:100, 10, replace = TRUE)
#[1] 86 54 46 25 65 42 76 49 46 50
sample(1:100, 10, replace = TRUE)
#[1] 44 44 90 39 46 14 41 52 36 63
Upvotes: 0
Views: 593
Reputation: 13103
@MrFlick posted a very clear and very helpful explanation for why my original attempt at a solution did not work.
Subsequently I arrived at a solution adequate for my purposes. I placed a set.seed
statement immediately after loading the RData
file into the original R
script. I saved the results as my target values.
Then I added the same set.seed
statement into my revised version of the R
script that never loads the RData
file.
Both approaches returned the same results.
Upvotes: 1