简体   繁体   English

如何将随机种子分配给 dplyr sample_n function?

[英]How do I assign a random seed to the dplyr sample_n function?

This is the "sample_n" from dplyr in R.这是 R 中 dplyr 中的“sample_n”。
https://dplyr.tidyverse.org/reference/sample.html https://dplyr.tidyverse.org/reference/sample.html

For reproducibility, I should place a seed so that someone else can get my exact results.为了重现性,我应该放置一个种子,以便其他人可以得到我的确切结果。

Is there a built-in way to set the seed for "sample_n"?是否有内置方法来设置“sample_n”的种子? Is this something that I do in the environment and "sample_n" responds to it?这是我在环境中所做的事情并且“sample_n”会响应它吗?

These are not built-into the "sample_n" function.这些不是内置在“sample_n”function 中的。

  • There is the environment "set.seed" function [1]有环境“set.seed” function [1]
  • There is a library 'withr' that creates a seed-containing wrapper for code [2]有一个库“withr”为代码创建了一个包含种子的包装器[2]

. .

The dplyr::sample_n documentation tells that: dplyr::sample_n文档告诉我们:

This is a wrapper around sample.int() to make it easy to select random rows from a table.这是 sample.int() 的包装器,可以轻松地从表中 select 随机行。 It currently only works for local tbls.它目前仅适用于本地 tbls。

so behind sample_n , sample.int is called, which means that the standard Random Number Generator is used, and that you can use set.seed for reproducibility.所以在sample_n之后,调用了sample.int ,这意味着使用了标准的随机数生成器,并且您可以使用set.seed进行重现。

Does this example help?这个例子有帮助吗? In it, I am using set.seed and the mtcars dataset.在其中,我使用set.seedmtcars数据集。

set.seed(1)
x <- mtcars
sample_n(x, 10)

sample_n(x, 10) #without set.seed()

set.seed(1)
x <- mtcars
sample_n(x, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM