jeudi 28 mai 2020

How do you get set.seed() and sample() to produce the same results

I have seen a couple posts on the use of the set.seed() function followed by the sample() function, but it is still not clear to me why sample() gives different results.

I have replicated a very simple example using the code below and get different results based on the respective R versions.

How do I get the same sample() result consistently as this is important when sharing the code to be reproducible on another machine / platform.

set.seed(47)
index3 = sample(rnorm(200), 200, replace = FALSE)
index3

Version of 1st run (Windows):

##                _                           
## platform       x86_64-w64-mingw32          
## arch           x86_64                      
## os             mingw32                     
## system         x86_64, mingw32             
## status                                     
## major          3                           
## minor          5.3                         
## year           2019                        
## month          03                          
## day            11                          
## svn rev        76217                       
## language       R                           
## version.string R version 3.5.3 (2019-03-11)
## nickname       Great Truth

Version of 2nd run (Mac):

##                _                           
## platform       x86_64-apple-darwin17.0     
## arch           x86_64                      
## os             darwin17.0                  
## system         x86_64, darwin17.0          
## status                                     
## major          4                           
## minor          0.0                         
## year           2020                        
## month          04                          
## day            24                          
## svn rev        78286                       
## language       R                           
## version.string R version 4.0.0 (2020-04-24)
## nickname       Arbor Day



Aucun commentaire:

Enregistrer un commentaire