Skip to contents

Take a specified number of groups of observations with fixed group size by sampling without replacement and returns the result along with tidyverse code used to generate it.

Usage

random_sample(data, n, sample_size)

Arguments

data

a dataframe to sample from

n

the number of groups to generate

sample_size

the size of each group specified in n

Value

a dataframe containing the random samples with tidyverse code attached

See also

Author

Owen Jin, Zhaoming Su

Examples

rs <- random_sample(iris, n = 5, sample_size = 3)
cat(code(rs))
#> iris |> dplyr::slice_sample(n = 5 * 3) |> dplyr::mutate(.group = factor(rep(   seq_len(5), each = 3)))
head(rs)
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species .group
#> 1          6.0         2.2          4.0         1.0 versicolor      1
#> 2          5.8         2.7          5.1         1.9  virginica      1
#> 3          6.5         3.2          5.1         2.0  virginica      1
#> 4          5.1         3.8          1.6         0.2     setosa      2
#> 5          5.0         3.6          1.4         0.2     setosa      2
#> 6          5.1         3.3          1.7         0.5     setosa      2