Different sample results using set.seed command?

≡放荡痞女 提交于 2021-01-29 05:40:27

问题


I want to use the sample function after setting set.seed with a fixed number. Unfortunately I obtain different results from a edx website. I'm wondering why I get different results. In the website they obtain 0.2706222. On my computer I obtain 0.3293778

I realized that the problem came from my sample function which do not returns me the same sample. Setting set.seed(1) and using sample(seq(1:5000), 3). My computer returns 1017 4775 2177 whereas it "should be" 1328 1861 2864

library(downloader) 
url <- "https://raw.githubusercontent.com/genomicsclass/dagdata/master/inst/extdata/femaleControlsPopulation.csv"
input_file <- basename(url)
download(url, destfile=input_file)
x <- unlist( read.csv(input_file) )
set.seed(1)
my_sample <- sample(x,5)
abs(mean(my_sample)-mean(x))

The expected output of the code computing the mean should be 0.2706222.

来源:https://stackoverflow.com/questions/56356858/different-sample-results-using-set-seed-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!