Generates a Sample from a set, with replacement

r_sample(sample, weights = NULL, ..., n = default_n(), .seed = NULL)

Arguments

sample

a set of values to choose from

weights

a vector of weights, must be the same length as sample, between 0 & 1

...

Unused

n

number of observations to generate. The default_n() function will provide a default value within context

.seed

One of the following:

  • NULL (default) will not change the current seed. This is the usual case for generating random numbers.

  • A numeric value. This will be used to set the seed before generating the random numbers. This seed will be stored with the results.

  • TRUE. A random seed value will be generated and set as the seed before the results are generated. Again, this will be stored with the results.

To extract the random seed from a previously generated set of values, use pull_seed()

Value

A vector of length n of the same type as sample

Examples

set_n(15) r_sample(c("blue", "red", "yellow"))
#> [1] "red" "blue" "yellow" "red" "red" "yellow" "blue" "blue" #> [9] "red" "red" "red" "red" "red" "red" "red"
r_sample(c("blue", "red", "yellow"), weights = c(1, 5, 1) )
#> [1] "blue" "red" "red" "red" "red" "red" "red" "red" #> [9] "yellow" "red" "blue" "blue" "red" "blue" "red"
r_sample(c("blue", "red", "yellow"), n = 10)
#> [1] "blue" "red" "blue" "blue" "blue" "yellow" "blue" "blue" #> [9] "yellow" "blue"