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

This is the "sample_n" from dplyr in R.

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"? Is this something that I do in the environment and "sample_n" responds to it?

These are not built-into the "sample_n" function.

  • There is the environment "set.seed" function [1]
  • There is a library 'withr' that creates a seed-containing wrapper for code [2]

.

1

2 Answers

The dplyr::sample_n documentation tells that :

This is a wrapper around sample.int() to make it easy to select random rows from a table. It currently only works for local 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.

1

Does this example help? In it, I am using set.seed and the mtcars dataset.

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) 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like